c# - URL 重写不起作用

标签 c# asp.net url-rewriting

我只是想不通为什么 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/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 .这可以完成您在问题中描述的内容。

关于c# - URL 重写不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38902123/

相关文章:

C#如何处理GUID返回类型异常?

C# DllImport 意外崩溃

c# - 如何在托管代码中获取 EIP 的当前值?

Django 和 Nginx 深层链接域(重写规则或 django 网址?)

javascript - 改变页面的URL而不改变页面

c# - 泛型类型的术语

c# - 如何将事件处理程序附加到运行时创建的 ASP.NET 控件?

c# - 结束标记 "text"没有匹配的开始标记

asp.net - IsAuthenticated 是假的!奇怪的行为+评论问题

php - 自定义模板的 WordPress URL 重写