为什么RewritePath更改浏览器网址? [英] Why RewritePath changes the Browser Url?

查看:394
本文介绍了为什么RewritePath更改浏览器网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET 4 HttpModule(请参见下面的代码)。当网址路径以 /1.0开头时,我希望Cassini / IIS转到MyService.svc。但是,我不想向用户显示 MyService.svc(即,不更新浏览器中的网址)。我希望用户看到 www.something.com/1.0。

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".

我非常确定RewriteUrl不应更改浏览器的url,但就我而言,确实可以。知道为什么吗?

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);
                }
            };
    }

由于网址中的句点/点,我无法使用ASP.NET路由(请参见带有句点的ASP.NET MVC路由ID。

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).

推荐答案

您需要对ASP.NET进行URL路由,并且自.NET 3.5 SP1起可用。

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.

为什么? MSDN表示:

Why? MSDN said this:


在ASP.NET路由中,您定义了包含占位符
的URL模式,这些占位符用于当您使用的值处理URL请求。在运行时,根据您定义的URL模式,将
紧随应用程序名称的URL片段解析为
离散值。以
为例,在
的请求中
http:// server / application / Products /表演/饮料,路由解析器
可以将值产品,表演和饮料传递给处理程序,以处理
请求。相反,在不受URL路由管理的请求中,/ Products / show / beverages片段中的
将被解释为应用程序中文件的路径

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.

您也可以使用URL模式以编程方式创建
与路线相对应的URL。这使您能够集中
在ASP.NET应用程序中创建超链接的逻辑。

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路由与其他URL重写方案不同。 URL
重写通过在将请求发送至Web页面之前实际更改URL
来处理传入请求。例如,使用URL重写的
应用程序可能会将URL从
/ Products / Widgets /更改为/Products.aspx?id=4。另外,URL重写
通常没有用于创建基于
您的模式的URL的API。在URL重写中,如果更改URL模式,则必须
手动更新包含原始URL的所有超链接。

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.

使用ASP.NET路由,URL处理传入请求
时,不会更改,因为路由可以从URL中提取值。当
必须创建URL时,可以将参数值传递到
为您生成URL的方法中。要更改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.

请参见 MSDN库中的ASP.NET路由

这篇关于为什么RewritePath更改浏览器网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆