IIS URL Rewrite For URL Link with =

2019-07-17 08:06发布

问题:

I am having an issue where someone has posted a URL for my asp.net mvc3 site that contains %3D instead of an equal sign after my id parameter like this: http://ultimategamedb.com/Article/Article?id%3D398210c1-529e-4909-9793-a11b8a832dcc . I have tried various rewrite rules but cannot seem the URL to rewrite correctly to: http://ultimategamedb.com/Article/Article?id=398210c1-529e-4909-9793-a11b8a832dcc . It is a problem because the URL with the %3D gives a 404 error. If possible I would like to create a rule that rewrite any URL with % encoding to the correct decoded values. If this is not possible I would just like to know how to rewrite my article URLs to ensure that a %3D will never show up for the equals sign in an Article URL again.

Could anyone explain to me how I would go about creating these rewrite rules? Thanks in advance!

回答1:

You have few options:

  1. Write your own provider which will replace %3D with =.

    Reference: http://www.iis.net/learn/extensions/url-rewrite-module/developing-a-custom-rewrite-provider-for-url-rewrite-module

  2. In Application_BeginRequest, replace your path:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        string path = HttpContext.Current.Request.Url.PathAndQuery;
    
        // Search for %3D and replace.
        path = path.Replace("%3D","=");
    
        HttpContext.Current.RewritePath(path);
    }