Search code examples
c#.netmodel-view-controllervirtual-directory

Why is my code not evaluated as expected inside of a tag attribute?


This code:

<% string path = Request.ApplicationPath.ToString(); %>
<link href="<%= path %>/Content/Site.css" rel="stylesheet" type="text/css" />

Returns this:

<link href="../Views/Shared/%3C%25=%20path%20%25%3E/Content/Site.css" rel="stylesheet" type="text/css" />

Whereas I was expecting this:

<link href="/foo/Content/Site.css" rel="stylesheet" type="text/css" />

Why did my code not return the expected path? How do I set up my scripts, css files, and images to be flexible if my virtual directory changes?


Solution

  • To answer your first question, what view engine are you using? Are you using MVC3, which I believe defaults to Razor and not Asp.Net for the view engine?

    To answer your second question, you should try this: <link href="<%: Url.Content( "~/Content/Site.css" ) %>" rel="stylesheet" type="text/css" />

    That should output what you want