简体   繁体   中英

Url rewrite with ASP.NET query string

I would like to rewrite URL with query string in ASP.NET. The URL looks like as following:

http://localhost:51392/productdetails.aspx?id=zdpMPZVXkDtjw92Crx7eew==

I would like to rewrite the url into something like this:

http://localhost:51392/Details/zdpMPZVXkDtjw92Crx7eew==

I have tried using the following method to rewrite the url (something like this):

 <urlMappings>
      <add url="~/Shop"   mappedUrl="~/shop.aspx"/>
</urlMappings>

But I'm not really sure how to properly map the URL with query string?? What is the easiest, or other method that can be used to achieve this??

Thanks!

PS guys this is how I'm trying to access the rewriten URL:

<a href='<%# "/productdetails.aspx?id=" + RL_DAL.RijndaelHelper.AES.Encrypt(Eval("ProductID").ToString(),"key_to_enrypt")  %>'><%# Eval("ProductName") %>

In asp.net, hosted on an IIS platform -

  1. Install the URL Rewrite module into IIS
  2. Add this into the web.config ( system.webServer ) section of the site

     <rewrite> <rules> <rule name="rewrite" stopProcessing="true"> <match url="productdetails.aspx" /> <conditions> <add input="{QUERY_STRING}" pattern="id=([a-zA-Z0-9=]+)" /> </conditions> <action type="Rewrite" url="/Details/{C:1}" appendQueryString="false" /> </rule> </rules> </rewrite> 

Then every request to /productdetails.aspx?id=zdpMPZVXkDtjw92Crx7eew== will be rewritten to http://localhost:51392/Details/zdpMPZVXkDtjw92Crx7eew== . But rewritten doesn't mean it will change for the user in their browser, just the destination on the server.

However, if you want to redirect it, change the rule to:

<rule name="redirect" stopProcessing="true">
  <match url="productdetails.aspx" />
  <conditions>
    <add input="{QUERY_STRING}" pattern="id=([a-zA-Z0-9=]+)" />
  </conditions>
  <action type="Redirect" url="/Details/{C:1}" redirectType="Permanent" appendQueryString="false" />
</rule>

This will do a permenent 301 redirect, to do a temporary 307 redirect change Permanent to Temporary on the action type.

See Redirect vs Rewrite for more information on the difference between the two.


With the redirect rule I gave:

The user enters /productdetails.aspx?id=zdpMPZVXkDtjw92Crx7eew== they are redirected to /Details/zdpMPZVXkDtjw92Crx7eew== and that URL must be capable of processing the request. This scencrio is typically used when moving a site to a new platform / URL structure.

To do the opposite, with the user entering /Details/zdpMPZVXkDtjw92Crx7eew== and the site processing the request on /productdetails.aspx?id=zdpMPZVXkDtjw92Crx7eew== use:

<rule name="rewrite">
    <match url="Details/([a-zA-Z0-9=]+)" />
    <action type="Rewrite" url="/productdetails.aspx?id={R:1}" />
</rule>

( Example on GitHub )

in global.asax

void RegisterRoutes(RouteCollection routes)
    {
         routes.MapPageRoute("Products","Products/{id}/{productname}",
                           "~/productdetails.aspx");
}

in products.aspx

<ahref='<%#string.Format("/Products/{0}/{1}",Eval("id"),Clean.CleanUrl(Eval("ProductName").ToString())) %>'><%# Eval("ProductName") %></a>

in productsdetails.aspx.cs

if (!IsPostBack)
        {
            if (RouteData.Values["id"] != null)
            {
               loadproduct();

            }
}

void loadproduct()
{
 select .............  where id= RouteData.Values["id"].ToString();
}

This is Clean Class

public static class Clean
    {
        public static string CleanURL(this string kelime)
        {
            if (kelime == "" || kelime == null) { return ""; }
            kelime = kelime.Replace("ş", "s");
            kelime = kelime.Replace("Ş", "S");
            kelime = kelime.Replace(".", "");
            kelime = kelime.Replace(":", "");
            kelime = kelime.Replace(";", "");
            kelime = kelime.Replace(",", "");
            kelime = kelime.Replace(" ", "-");
            kelime = kelime.Replace("!", "");
            kelime = kelime.Replace("(", "");
            kelime = kelime.Replace(")", "");
            kelime = kelime.Replace("'", "");
            kelime = kelime.Replace("ğ", "g");
            kelime = kelime.Replace("Ğ", "G");
            kelime = kelime.Replace("ı", "i");
            kelime = kelime.Replace("I", "i");
            kelime = kelime.Replace("ç", "c");
            kelime = kelime.Replace("ç", "C");
            kelime = kelime.Replace("ö", "o");
            kelime = kelime.Replace("Ö", "O");
            kelime = kelime.Replace("ü", "u");
            kelime = kelime.Replace("Ü", "U");
            kelime = kelime.Replace("`", "");
            kelime = kelime.Replace("=", "");
            kelime = kelime.Replace("&", "");
            kelime = kelime.Replace("%", "");
            kelime = kelime.Replace("#", "");
            kelime = kelime.Replace("<", "");
            kelime = kelime.Replace(">", "");
            kelime = kelime.Replace("*", "");
            kelime = kelime.Replace("?", "");
            kelime = kelime.Replace("+", "-");
            kelime = kelime.Replace("\"", "-");
            kelime = kelime.Replace("»", "-");
            kelime = kelime.Replace("|", "-");
            kelime = kelime.Replace("^", "");
            return kelime;
        }
    }

In global.asax.cs file, try to add code in this way.

    void Application_BeginRequest(object sender, EventArgs e) {

    string fullpath = Request.Url.ToString();

    if (fullpath.Contains("/Details/zdpMPZVXkDtjw92Crx7eew==")) {
        Context.RewritePath("/productdetails.aspx?id=zdpMPZVXkDtjw92Crx7eew==");
    }
   }

And then in the Productdetails.aspx.cs file, you may try to read the Id.

Label1.Text = Request.QueryString["id"];

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