为什么 RewritePath 会改变浏览器 URL?

发布于 2024-12-07 08:21:55 字数 875 浏览 1 评论 0原文

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

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

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

PS 由于 URL 中的句点/点,我无法使用 ASP.NET 路由(请参阅 带句点的 ASP.NET MVC 路由 ID)。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱ヾ落雨殇 2024-12-14 08:21:55

看起来您遇到了与此处描述的相同的问题:
ASP.NET RewritePath 未按预期工作/URL在浏览器中更改

在 url 中添加尾部斜杠:

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

另外,我不确定 WCF 引擎是否会为您保留 PathInfo。可能您必须将 URL 作为查询字符串传递参数。

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.

魂归处 2024-12-14 08:21:55

您需要 ASP.NET 的 url 路由,它从 .NET 3.5 SP1 开始可用。

对于你的情况,我认为“路由”而不是重写更容易,而且使用起来更简单。

为什么? MSDN 是这么说的:

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

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

ASP.NET 路由与 URL 重写

ASP.NET 路由不同于其他 URL 重写方案。网址
通过实际更改 URL 重写处理传入请求
在将请求发送到网页之前。例如,一个
使用 URL 重写的应用程序可能会更改 URL
/Products/Widgets/ 到 /Products.aspx?id=4。另外,URL重写
通常没有用于创建基于以下内容的 URL 的 API
你的模式。在 URL 重写中,如果更改 URL 模式,则必须
手动更新所有包含原始 URL 的超链接。

使用 ASP.NET 路由,传入请求时 URL 不会更改
被处理,因为路由可以从 URL 中提取值。当你
必须创建一个 URL,您将参数值传递到一个方法中
为您生成 URL。要更改 URL 模式,您可以在
一个位置以及您在应用程序中创建的所有链接
基于该模式的将自动使用新模式。

请参阅 MSDN 中的 ASP.NET 路由图书馆。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文