我有一个 ASP.NET 4 HttpModule(见下面的代码)。当 url 路径以“/1.0”开头时,我希望 Cassini/IIS 转到 MyService.svc。但是,我不想向用户显示“MyService.svc”(即浏览器中的 url 没有更新)。我希望用户看到“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);
}
};
}
附言由于 Url 中的句点/点,我无法使用 ASP.NET 路由(请参阅 ASP.NET MVC Route IDs with a period)。
最佳答案
看起来您遇到的问题与此处描述的相同: ASP.NET RewritePath not working as expected / URL in browser changing
将尾部斜杠添加到 url:
ctx.RewritePath("~/MyService.svc/", "this/is/a/path", string.Empty, false);
此外,我不确定 WCF 引擎是否会为您保留 PathInfo。可能您必须使用 URL 作为 QueryString 传递参数。
关于c# - 为什么 RewritePath 会更改浏览器 Url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7606252/