Search

ASP.NET MVC Url.Content not working in Master pages. by NaZAf

Closed
as By Design Help for as By Design

1
0
Sign in
to vote
Type: Bug
ID: 573038
Opened: 7/3/2010 7:50:20 PM
Access Restriction: Public
0
Workaround(s)
0
User(s) can reproduce this bug
When using a string variable as parameter to the Url.Content helper method, it does not return the correct value (file path) in master pages.
Details (expand)

Product Language

English

Visual Studio Version

Visual Studio 2010

Operating System

Windows XP

Operating System Language

English

Steps to Reproduce

1. Create any ASP.NET MVC project.
2. Create any controller and use a view that has a masterpage.
3. In the master page header use this code:
<head runat="server">
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<% var css = "~/Stylesheet/styles.css"; %>
<link href="<%= Url.Content(css) %>" type="text/css" rel="Stylesheet" />
</head>

Actual Results

....
<link href="Views/Shared/%3C%25=%20Url.Content(css)%20%25%3E" type="text/css" rel="Stylesheet" />
....

Expected Results

....
<link href="/Stylesheet/styles.css" type="text/css" rel="Stylesheet" />
....
      You can indicate your satisfaction with how Microsoft handled this issue by completing this quick 3 question survey.

 

File Attachments
0 attachments
Sign in to post a comment.
Posted by NaZAf on 7/8/2010 at 7:31 PM
I used the following code without modifying anything else, and it worked pretty well:

<% Response.Write("<link href='" + Url.Content(css) + "' rel=\"stylesheet\" type=\"text/css\" />"); %>

I Hope Razor will not have this drawback!!

Thanks.
Nadeem.
Posted by Microsoft on 7/8/2010 at 1:39 PM
Hi,

The Web Form view engine in ASP.NET MVC has some "interesting" behavior in it because of how ASP.NET Page and Controls work. One of those behaviors is that within a <head runat="server"> tag, all <link> tags will go through special server processing. Because of this special processing you cannot have <%= code nuggets %> inside the <link> tag.

There are several workarounds that I can suggest:
1. If you know the path ahead of time, you can use a relative path or app-root path in the <link> tag and ASP.NET will resolve it. For example, you can point to "~/styles/site.css" and the "~" will get resolved by the server.

2. Remove the runat="server" from the <head> tag to enable the code nugget in the <link> tag. There might be other side-effects of this change.

3. In the page's OnInit or OnLoad event programmatically create an HtmlLink control, set the appropriate attributes on it, and add it to the Page's Header. Even though some people don't like putting code in their MVC views, this is all just rendering code and not business logic or application logic, so it's generally considered to be acceptable.

Thanks,
The ASP.NET Team
Posted by Microsoft on 7/4/2010 at 8:39 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 7/4/2010 at 4:05 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.