为什么 RewritePath 会改变浏览器 URL?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您遇到了与此处描述的相同的问题:
ASP.NET RewritePath 未按预期工作/URL在浏览器中更改
在 url 中添加尾部斜杠:
另外,我不确定 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:
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.
您需要 ASP.NET 的 url 路由,它从 .NET 3.5 SP1 开始可用。
对于你的情况,我认为“路由”而不是重写更容易,而且使用起来更简单。
为什么? MSDN 是这么说的:
请参阅 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:
See ASP.NET Routing in MSDN Library.