我只是想不通为什么 RewritePath 方法在这段代码中不起作用。 当我尝试从 ProductPage.aspx 页面浏览项目时,地址栏中的 URL 仍显示为 http://localhost:44789/ProductPage.aspx而不是像这样: http://localhost:44789/ProductPage.aspx/?color= "
这是我的代码:
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]);
}
}
}
更新: 我仍在努力解决这个问题。我试图在其他操作系统不同的机器上运行这段代码,但仍然没有成功。
最佳答案
简短回答:它不会更改浏览器地址栏中的 URL。这是一个内部重定向。
长答案请阅读 http://www.dotnetperls.com/rewritepath和 https://msdn.microsoft.com/en-us/library/system.web.httpcontext.rewritepath(v=vs.110).aspx .
您应该查看 IIS URL 重写模块 http://www.iis.net/downloads/microsoft/url-rewrite .这可以完成您在问题中描述的内容。
关于c# - URL 重写不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38902123/