c# - 为什么 RewritePath 会更改浏览器 Url?

标签 c# asp.net url-rewriting httpmodule cassini

我有一个 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/

相关文章:

c# - 如何在 C# 中获取 ip 地址的 fqdn?

asp.net - asp.net 文本框的默认值 -> TextMode = 密码

IIS10 URL Rewrite 2.1 双编码问题

.htaccess - 在 .htaccess 中如何获取特定值

php - htaccess 重写不带 .php 扩展名的 URL 到文件

c# - 如何使用 mvc4 C# 在我的 linq 中将多个表包含到实体急切加载中

c# - foreach抛出异常,为什么?

c# - 我什么时候应该使用延迟加载以及它应该延迟到什么程度

asp.net - <noscript> 中的验证器导致 JavaScript 错误

asp.net - 如何将自定义属性设置为 asp.net 文字并在后面的代码中读取它?