URL rewriting makes all the links of same patterns after visiting any rewritten link in ASP.NET

StackOverflow https://stackoverflow.com/questions/5841671

سؤال

I am using UrlRewriter to rewrite my urls in an ASP.NET application. Everything works fine and the work which I need to do is working ok.

I want to redirect ~/product/45/something to ~/show_product_details.aspx?current_prod=45

and it's working fine. But the problem is when I request ~/product/45/something, I am getting the page from ~/show_product_details.aspx?current_prod=45. After that when I click some link like ~/home.aspx, it again redirects me to ~/product/45/home.aspx.

Please suggest how to fix this. I am using this rule:

          <add name="Gallery1" virtualUrl="^~/product/(.*)/(.*)" 
                 rewriteUrlParameter="ExcludeFromClientQueryString"
                destinationUrl="~/show_product_details.aspx?current_prod=$1"
         ignoreCase="true" />   
هل كانت مفيدة؟

المحلول

It sounds like the URLs in your ASP.NET application are not resolved to the root of the application, but rather are relative links, causing the folder structure to be taken into account.

If you are using ASP.NET Web Forms you need to ensure you either call ResolveUrl("~/home.aspx") when outputting the URL, or ensure that the hyperlink is runat=server, for example:

<a runat="server" href="~/home.aspx">Home</a>

OR:

<a href='<%# ResolveUrl("~/home.aspx") %>'>Home</a>

Another way you can resolve this is to have a base reference in your HTML, which tells the browser that ALL links on the page must be rooted at the specified path:

In the <head> section:<base href='http://www.yourwebsite.com/' />

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top