2016-08-11 156 views
2

我只是不明白为什么RewritePath方法在这段代码中不起作用。 当我尝试从浏览网页ProductPage.aspx项目,在地址栏中的网址仍显示为http://localhost:44789/ProductPage.aspx的而不是这样的: http://localhost:44789/ProductPage.aspx/?color=URL重写不起作用

这里是我的代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

/// <summary> 
/// Summary description for GetProductInfo 
/// </summary> 
public class GetProductInfo:IHttpModule 
{ 
    public GetProductInfo() 
    { 
     // 
     // TODO: Add constructor logic here 
     // 
    } 

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

    public void Init(HttpApplication context) 
    { 
     context.BeginRequest += Context_BeginRequest; 

    } 

    private void Context_BeginRequest(object sender, EventArgs e) 
    { 
     HttpApplication App = sender as HttpApplication; 

     if (App.Request.Path.Contains("ProductPage.aspx")) 
     { 

      string[] Parts = App.Request.Path.Split('/'); 
      App.Response.Write(Parts.Length); 
      if (Parts.Length < 3) 
       App.Context.RewritePath("ProductPage.aspx/?Color="); 
      else 
       App.Context.RewritePath("ProductPage.aspx?color=" + Parts[Parts.Length - 1]); 

     } 
    } 
} 

更新: 我仍然试图解决这个问题,我试图在不同操作系统的其他机器上运行此代码仍然没有运气

+1

你有没有进入代码并调试它?它是否正确地击中了你的重写代码?它是通过它吗? –

+0

是的,我做到了。其实我在这里看到的东西,我不知道如何处理它: 当我检查app.context我看到了这个:App.Context.IsWebSocketRequest'App.Context.IsWebSocketRequest'抛出了一个异常'System.InvalidOperationException ' 你能帮我解决这个错误吗? –

回答

0

简短的回答:它不会更改浏览器的地址栏中的URL。这是一个内部重定向。

长答案阅读http://www.dotnetperls.com/rewritepathhttps://msdn.microsoft.com/en-us/library/system.web.httpcontext.rewritepath(v=vs.110).aspx

您应该查看IIS URL重写模块http://www.iis.net/downloads/microsoft/url-rewrite。这可以做你在你的问题中描述的。

+0

非常感谢。那就是问题所在,误解了这种方法的真正功能。 –