Pregunta

I have an ASP.NET 4 HttpModule (see code below). When the url path starts with "/1.0" I want Cassini/IIS to go to MyService.svc. However, I don't want to show "MyService.svc" to the user (i.e. no update to the url in the browser). I want the user to see "www.something.com/1.0".

I was pretty sure that RewriteUrl isn't supposed to change the browser url, but in my case it does. Any idea why?

    public void Init(HttpApplication context)
    {
        context.BeginRequest +=
            delegate
            {
                HttpContext ctx = HttpContext.Current;
                const string BasePath = "~/1.0";
                if (path.StartsWith(BasePath, StringComparison.OrdinalIgnoreCase))
                {
                    ctx.RewritePath("~/MyService.svc", "this/is/a/path", string.Empty, false);
                }
            };
    }

P.S. I cannot use ASP.NET Routing because of the period/dot in the Url (see ASP.NET MVC Route IDs with a period).

¿Fue útil?

Solución

You need url routing of ASP.NET, and it's available since .NET 3.5 SP1.

For your case, I think it's easier to "route" instead of rewriting, and it's simpler to use.

Why? MSDN said this:

In ASP.NET routing, you define URL patterns that contain placeholders for values that are used when you handle URL requests. At run time, the pieces of the URL that follow the application name are parsed into discrete values, based on a URL pattern that you have defined. For example, in the request for http://server/application/Products/show/beverages, the routing parser can pass the values Products, show, and beverages to a handler for the request. In contrast, in a request that is not managed by URL routing, the /Products/show/beverages fragment would be interpreted as the path of a file in the application.

You can also use the URL patterns to programmatically create URLs that correspond to the routes. This enables you to centralize the logic for creating hyperlinks in your ASP.NET application.

ASP.NET Routing versus URL Rewriting

ASP.NET routing differs from other URL rewriting schemes. URL rewriting processes incoming requests by actually changing the URL before it sends the request to the Web page. For example, an application that uses URL rewriting might change a URL from /Products/Widgets/ to /Products.aspx?id=4. Also, URL rewriting typically does not have an API for creating URLs that are based on your patterns. In URL rewriting, if you change a URL pattern, you must manually update all hyperlinks that contain the original URL.

With ASP.NET routing, the URL is not changed when an incoming request is handled, because routing can extract values from the URL. When you have to create a URL, you pass parameter values into a method that generates the URL for you. To change the URL pattern, you change it in one location, and all the links that you create in the application that are based on that pattern will automatically use the new pattern.

See ASP.NET Routing in MSDN Library.

Otros consejos

Looks like you have the same problem as described here: ASP.NET RewritePath not working as expected / URL in browser changing

Add the trailing slash to the url:

ctx.RewritePath("~/MyService.svc/", "this/is/a/path", string.Empty, false);

Also, I'm not sure if WCF engine would preserve PathInfo for you. Possibly you'll have to pass parameters with the URL as QueryString.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top