我只是想不通为什么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]);
}
}
}
更新: 我还在努力解决这个问题。我试图在其他机器上运行此代码与不同的操作系统仍然没有运气。
答案 0 :(得分:0)
简答:它不会更改浏览器地址栏中的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。这可以做你在问题中描述的内容。