0

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?

Jake
  • 4,829
  • 2
  • 33
  • 44

2 Answers2

3

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

Andy
  • 8,432
  • 6
  • 38
  • 76
  • Thanks. That's exactly what I was looking for. As for view engine, how do I determine which one I'm using? – Jake Mar 08 '11 at 18:30
  • In your global.asax you can set the view engine. If there's no code in there, you're probably using the default. I think in MVC3 is Razor by default, earlier versions use WebForms. You can check this out to find many other view engines: http://stackoverflow.com/questions/1451319/asp-net-mvc-view-engine-comparison – Andy Mar 08 '11 at 20:31
  • I know this question is 5 years old, but for the record, I was using WebForms. :) – Jake Nov 16 '16 at 15:19
0

Virtual paths can be specified using the ~ (tilde) prefix.

Simen S
  • 3,210
  • 18
  • 24