ASP.NET RewritePath未正常工作/ URL浏览器改变 [英] ASP.NET RewritePath not working as expected / URL in browser changing

查看:261
本文介绍了ASP.NET RewritePath未正常工作/ URL浏览器改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试重写ASP.NET中的URL我发现,在用户的浏览器的URL变化。我使用WCF REST服务,我想改变,你访问的URL的方式。请参阅下面的code的例子。

When I try to rewrite a URL in ASP.NET I'm finding that the URL changes on the user's browser. I'm using WCF REST services and I want to change the way that you access URLs. See the code example below.

我有一个请求拦截一个HttpModule。

I have an HttpModule that is intercepting requests.

public class FormatModule : IHttpModule
{
    #region IHttpModule Members

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public void Init(HttpApplication application)
    {
        application.BeginRequest += new EventHandler(application_BeginRequest);
    }

    void application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        if (context.Request.RawUrl.Contains(".pox")) 
            context.RewritePath("~/Lab1Service.svc?format=pox", false);
        else if (context.Request.RawUrl.Contains(".json")) 
            context.RewritePath("~/Lab1Service.svc?format=json", false);
    }

    #endregion
}

当用户访问该网址在浏览器出现问题。

The problem occurs when the users visits the URL in their browser.

的http://localhost/Lab1Service.svc.pox 时,而不是在浏览器的URL变化 HTTP:?//localhost/Lab1Service.svc格式=痘

http://localhost/Lab1Service.svc.pox, instead the URL changes in the browser to http://localhost/Lab1Service.svc?format=pox.

推荐答案

我解决了这个。看来,如果你不包括.svc扩展后尾部的反斜杠的URL重定向而不是重写。

I resolved this. It appears that if you don't include the trailing backslash after the .svc extension the URL REDIRECTS instead of REWRITING.

这是我原来的

context.RewritePath("~/Lab1Service.svc?format=pox", false);

这是修正版本(注意.SVC后斜杠)

This is the corrected version (notice the forward slash after the .svc)

context.RewritePath("~/Lab1Service.svc/?format=pox", false);

这篇关于ASP.NET RewritePath未正常工作/ URL浏览器改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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