Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Adding runat="server" to HTML tags to get relative path in ASP.net an elegant solution?

Tags:

html

asp.net

I've got a couple of ASP.Net Usercontrols that I am using in different locations of my new website. These usercontrols had links like this :

<a href="daily/panchang/"></a>

If the usercontrol is used in pages in various subdirectories the relative path just doesn't work, and I don't want to provide my full website name in the path. So I did this

<a href="~/daily/panchang/" runat="server">

and now the ASP.Net '~' marker works correctly to resolve the root path.

Is it okay to mark all my HTML tags where I have the need to resolve the root path with runat="server" or do you know of a better, HTML way?

Thanks

like image 369
Cyril Gupta Avatar asked Dec 30 '08 12:12

Cyril Gupta


People also ask

What is the use of Runat server in asp net?

The runat="server" tag in ASP.NET allows the ability to convert/treat most any HTML element as a server-side control that you can manipulate via code at generation time. Some controls have explicit implementations, others simply revert to a generic control implementation.

What is Runat server HTML?

The runat="server" attribute indicates that the form should be processed on the server. It also indicates that the enclosed controls can be accessed by server scripts: <form runat="server"> ...HTML + server controls.


2 Answers

I won't say whether it's an elegant solution, I'll just point out an alterantive within System.Web:

<a href="<%= VirtualPathUtility.ToAbsolute("~/daily/panchang/") %>">
like image 117
Cristian Libardo Avatar answered Nov 14 '22 22:11

Cristian Libardo


You should use a base tag to define the root of your application and make all links relative like this :

<head>
    <base href="<%= Request.ApplicationPath %>" />
</head>
...
<a href="daily/panchang/"></a> <!-- this now points to ~/daily/panchang/ -->
like image 37
Diadistis Avatar answered Nov 14 '22 21:11

Diadistis