Chitika

Sunday, October 14, 2012

Url problems in asp.net, solve using ResolveUrl

Many times we use runat="server" to refer images and style sheet.
This might be an easy trick, but I ran into this issue a while ago; we can not specify the runat="server" attribute on a JavaScript file link as we do for images and style sheet.

<link id="mainSheet" runat="server" href="~/Images/styles.css"
rel="stylesheet" type="text/css" />


By using runat=server the href of the link will automatically be resolved by the asp.net in we have placed ~/ in the path. This gives us advantages when we are linking this into a control and the control could be used by other pages places at a different directory level.

Same problem arise with using linked JavaScript. But this cannot be solved by using runat=server tag. Here is an alternative way to overcome this problem.

<script language="javascript" src="<%=ResolveUrl("~/App_Themes/MainTheme/jquery.js")%>" type="text/javascript"></script>

For more information about Path or url in asp.net see this



No comments:

Post a Comment