flying227 flying227 - 26 days ago 9
ASP.NET (C#) Question

How to generate url for href in aspx page

I appologize if this is a basic question, but it has been a long times since I have used Web Forms. How does one generate a URL in a master page's aspx page so that the reference works everywhere across the app?

Here is an example from a menu in a master page:

<a href="Admin/AddVendor.aspx">Add Vendor</a>


The problem is that if I leave the URL as it is in the master page, then when someone is already on a page that resides in the Admin folder, then this url no longer works. I need to path the URL from root every time, but forget how to do this in Web Forms.

Answer

Try using this, which maps to your website's root directory:

<a href="/Admin/AddVendor.aspx">Add Vendor</a>

EDIT : In that case, you could do this:

<a href="<%= Page.ResolveUrl("~/Admin/AddVendor.aspx") %>">Add Vendor</a>

It would be far easier to just use the asp.net Hyperlink control though

<asp:HyperLink ID="Link1" runat="server" NavigateUrl="~/Admin/AddVendor.aspx" Text="Add Vendor" />
Comments