Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I specify links in ASP.NET user controls when using IIS 7 URL Rewrite?
    primarykey
    data
    text
    <p>I am having some difficulty getting URLs in user controls to work right in my ASP.NET 4 web site when using URL Rewrite rules. Specifically, it appears that the HyperLink element is changing my link urls. I am not sure if I am using the proper technique or missing something.</p> <p>I have a physical directory structure that looks like this:</p> <ul> <li>Root\ <ul> <li>page1.aspx</li> <li>page2.aspx</li> <li>styles.css</li> <li>Masters\ <ul> <li>site.master</li> </ul></li> <li>UserControls\ <ul> <li>header.ascx</li> </ul></li> </ul></li> </ul> <p>page1.aspx uses site.master and site.master contains header.ascx.</p> <p>I have also set up URL Rewrite module so that requests like:</p> <ul> <li><a href="http://localhost/mysite/page/a" rel="nofollow">http://localhost/mysite/page/a</a></li> <li><a href="http://localhost/mysite/page/a/b" rel="nofollow">http://localhost/mysite/page/a/b</a></li> <li><a href="http://localhost/mysite/page/a/b/c" rel="nofollow">http://localhost/mysite/page/a/b/c</a></li> </ul> <p>are redirected to:</p> <ul> <li><a href="http://localhost/mysite/page1.aspx?p=a" rel="nofollow">http://localhost/mysite/page1.aspx?p=a</a></li> <li><a href="http://localhost/mysite/page1.aspx?p=a/b" rel="nofollow">http://localhost/mysite/page1.aspx?p=a/b</a></li> <li><a href="http://localhost/mysite/page1.aspx?p=a/b/c" rel="nofollow">http://localhost/mysite/page1.aspx?p=a/b/c</a></li> </ul> <p>That all works fine. </p> <p>However, some of the links in the user control are not resolving correctly.</p> <p>Here is the site.master:</p> <pre><code>&lt;%@ Master Language="C#" AutoEventWireup="true" CodeFile="site.master.cs" Inherits="Masters_site" %&gt; &lt;%@ Register Src="../UserControls/header.ascx" TagName="Header" TagPrefix="uc1" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head id="Head1" runat="server"&gt; &lt;base href="http://localhost/mysite/"&gt;&lt;/base&gt; &lt;link href="../styles.css" type="text/css" rel="stylesheet" /&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;uc1:Header ID="Header1" runat="server" /&gt; &lt;asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Note that I am using the HTML <strong>&lt;base></strong> tag to ensure that content is always retrieved from the root of the web site. This makes it easy to use and links in the css that are independent of the page's path.</p> <p>And here is the user control with some test links:</p> <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeFile="header.ascx.cs" Inherits="header" %&gt; &lt;a href="page2.aspx"&gt;Page 2&lt;/a&gt; &lt;asp:HyperLink ID="lnkPage1" runat="server" NavigateUrl="page2.aspx"&gt;Page 2&lt;/asp:HyperLink&gt; &lt;asp:HyperLink ID="lnkPage2" runat="server" NavigateUrl="/page2.aspx"&gt;Page 2&lt;/asp:HyperLink&gt; &lt;asp:HyperLink ID="lnkPage3" runat="server" NavigateUrl="../page2.aspx"&gt;Page 2&lt;/asp:HyperLink&gt; &lt;asp:HyperLink ID="lnkPage4" runat="server" NavigateUrl="~/page2.aspx"&gt;Page 2&lt;/asp:HyperLink&gt; </code></pre> <p>What I want is a link that just renders as "page2.aspx" so that the browser converts that to <a href="http://localhost/mysite/page2.aspx" rel="nofollow">http://localhost/mysite/page2.aspx</a>.</p> <p>While that works great for the &lt;a> link, it does not work like I would like for the HyperLink (or the link to the stylesheet in the master, for that matter). </p> <p>When going straight to <a href="http://localhost/mysite/page1.aspx" rel="nofollow">http://localhost/mysite/page1.aspx</a> with no rewriting I get this for the HyperLink (Original, rendered, used by browser):</p> <ul> <li>"page2.aspx" ==> "UserControls/page2.aspx" ==> <a href="http://localhost/UserControls/page2.aspx" rel="nofollow">http://localhost/UserControls/page2.aspx</a> (DOES NOT WORK)</li> <li>"/page2.aspx" ==> "/page2.aspx" ==> <a href="http://localhost/page2.aspx" rel="nofollow">http://localhost/page2.aspx</a> (DOES NOT WORK)</li> <li>"../page2.aspx" ==> "page2.aspx" ==> <a href="http://localhost/mysite/page2.aspx" rel="nofollow">http://localhost/mysite/page2.aspx</a> (OK)</li> <li>"~/page2.aspx" ==> "page2.aspx" ==> <a href="http://localhost/mysite/page2.aspx" rel="nofollow">http://localhost/mysite/page2.aspx</a> (OK)</li> </ul> <p>When I go to <a href="http://localhost/mysite/page/a" rel="nofollow">http://localhost/mysite/page/a</a>:</p> <ul> <li>"page2.aspx" ==> "UserControls/page2.aspx" ==> <a href="http://localhost/UserControls/page2.aspx" rel="nofollow">http://localhost/UserControls/page2.aspx</a> (DOES NOT WORK)</li> <li>"/page2.aspx" ==> "/page2.aspx" ==> <a href="http://localhost/page2.aspx" rel="nofollow">http://localhost/page2.aspx</a> (DOES NOT WORK)</li> <li>"../page2.aspx" ==> "page2.aspx" ==> <a href="http://localhost/mysite/page2.aspx" rel="nofollow">http://localhost/mysite/page2.aspx</a> (OK)</li> <li>"~/page2.aspx" ==> "page2.aspx" ==> <a href="http://localhost/mysite/page2.aspx" rel="nofollow">http://localhost/mysite/page2.aspx</a> (OK)</li> </ul> <p>When I go to <a href="http://localhost/mysite/page/a/b" rel="nofollow">http://localhost/mysite/page/a/b</a>:</p> <ul> <li>"page2.aspx" ==> "../UserControls/page2.aspx" ==> <a href="http://localhost/UserControls/page2.aspx" rel="nofollow">http://localhost/UserControls/page2.aspx</a> (DOES NOT WORK)</li> <li>"/page2.aspx" ==> "/page2.aspx" ==> <a href="http://localhost/page2.aspx" rel="nofollow">http://localhost/page2.aspx</a> (DOES NOT WORK)</li> <li>"../page2.aspx" ==> "../page2.aspx" ==> <a href="http://localhost/page2.aspx" rel="nofollow">http://localhost/page2.aspx</a> (DOES NOT WORK)</li> <li>"~/page2.aspx" ==> "../page2.aspx" ==> <a href="http://localhost/page2.aspx" rel="nofollow">http://localhost/page2.aspx</a> (DOES NOT WORK)</li> </ul> <p>I can't the HyperLink control to stop changing the link.</p> <p>I have a similar problem with that in the site.master where it can't find the file when rewriting with /a/b.</p> <p>Question 1: How do I stop a links from changing the URL? Question 2: Is there a better technique for accomplishing the above without the side effects?</p> <p>I should also mention that I am only having this problem with the IIS7 URL Rewrite module. I did not have this problem with the exact same web site and Helicon ISAPI Rewrite so I am thinking that URL Rewrite is not playing nice with ASP.NET.</p> <p>One other thing; when I post this to the production server, the url changes to "http://mysite" so any solution needs to take that into account.</p> <p>Thanks in advance for your help!</p> <p><strong>UPDATE 10/28/2011</strong></p> <p>I was asked to show the rewrite rule for this example:</p> <pre><code>&lt;rewrite&gt; &lt;rules&gt; &lt;clear /&gt; &lt;rule name="Rewrite category" stopProcessing="true"&gt; &lt;match url="^page/(.*)$" /&gt; &lt;conditions logicalGrouping="MatchAll" trackAllCaptures="false"&gt; &lt;add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /&gt; &lt;add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /&gt; &lt;/conditions&gt; &lt;action type="Rewrite" url="page1.aspx?p={R:1}" appendQueryString="false" /&gt; &lt;/rule&gt; &lt;/rules&gt; &lt;/rewrite&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload