C# URL重写不起作用
我就是不明白为什么重写路径方法在这段代码中不起作用。 当我尝试从ProductPage.aspx页面浏览项目时,地址栏中的URL仍然显示为,而不是如下所示: " 这是我的密码:C# URL重写不起作用,c#,asp.net,url-rewriting,C#,Asp.net,Url Rewriting,我就是不明白为什么重写路径方法在这段代码中不起作用。 当我尝试从ProductPage.aspx页面浏览项目时,地址栏中的URL仍然显示为,而不是如下所示: " 这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for GetProductInfo /// </sum
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]);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
///
///GetProductInfo的摘要说明
///
公共类GetProductInfo:IHttpModule
{
公共GetProductInfo()
{
//
//TODO:在此处添加构造函数逻辑
//
}
公共空间处置()
{
抛出新的NotImplementedException();
}
公共void Init(HttpApplication上下文)
{
context.BeginRequest+=context_BeginRequest;
}
私有void上下文_BeginRequest(对象发送方,事件参数e)
{
HttpApplication App=发送方作为HttpApplication;
if(App.Request.Path.Contains(“ProductPage.aspx”))
{
string[]Parts=App.Request.Path.Split('/');
应用响应写入(部分长度);
如果(零件长度<3)
App.Context.RewritePath(“ProductPage.aspx/?Color=”);
其他的
App.Context.RewritePath(“ProductPage.aspx?color=“+Parts[Parts.Length-1]);
}
}
}
更新:
我仍在尝试解决此问题。我尝试在其他具有不同操作系统的计算机上运行此代码,但仍然没有成功。简短回答:它不会更改浏览器地址栏中的URL。这是一个内部重定向 要获得详细的答案,请阅读并阅读
您应该查看IIS URL重写模块。这可以完成您在问题中描述的操作。您是否已进入代码并对其进行了调试?它是否正确地命中了您的重写代码?它是否正在逐步执行?是的。实际上,我在这里看到了一些我不知道如何处理的内容:当我检查app.context时,我看到了以下内容:App.Context.IsWebSocketRequest“App.Context.IsWebSocketRequest”引发了一个类型为“System.InvalidOperationException”的异常,您能帮我解决此错误吗?非常感谢。这就是问题所在,误解了此方法的真正功能。