简体   繁体   中英

URL Rewriting not working

I just can't figure out why RewritePath method is not working in this code. When I try to browse the Project from ProductPage.aspx page, the URL in address bar is still shown as http://localhost:44789/ProductPage.aspx instead of being like this: http://localhost:44789/ProductPage.aspx/?color= "

here is my code :

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]);

        }
    }
}

update: I`m still trying to solve this problem. i tried to run this code on other machines with different OS still no luck.

Short answer: It won't change the URL in the address bar of the browser. It is an internal redirect.

For the long answer read http://www.dotnetperls.com/rewritepath and https://msdn.microsoft.com/en-us/library/system.web.httpcontext.rewritepath(v=vs.110).aspx .

You should look into the IIS URL Rewrite module http://www.iis.net/downloads/microsoft/url-rewrite . That can do what you describe in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM