0

This worked yesterday, and I must have changed something at some point, but the url path to the stylesheet no longer works with the following code:

@section Styles {
    @{
        string path = Url.Content("~") + Model.CssPath;
        <link href="path" rel="stylesheet" type="text/css" />
    }
}

When I write out 'path' to the page, it's //Content/site.css. If I browse to http://localhost:59278/Content/site.css, it works. But, the link path is looking at http://Content/site.css.

Why?

8
  • 1
    Shouldn't it be href="@path"? Commented Aug 6, 2015 at 8:46
  • @haim770 I thought so too, but it doesn't make a difference... if I look at the page's source, I still see <link href="//Content/site.css" rel="stylesheet" type="text/css" /> Commented Aug 6, 2015 at 8:48
  • Be aware of the caching. This is also likely to result in another behavior. Commented Aug 6, 2015 at 8:48
  • @CSharper I cleared the browser cache (for always), still the same. Commented Aug 6, 2015 at 8:51
  • 1
    What is the value of Model.CssPath? Commented Aug 6, 2015 at 8:54

2 Answers 2

1

When you use:

Url.Content("~")

it gives you the root path of your site, always ending in /.

In this case, it is '/' as your local server doesn't have any subpath. When you deploy to another server, it may be "/MySite/SubPath/etc/" (still ending in '/').

As stated in the comments, Model.CssPath is "/Content/site.css"

So locally it is:

"/" + "/Content/site.css" = "//Content/site.css"

but when deployed, might be:

"/MySite/SubPath/etc/" + "/Content/site.css" = "/MySite/SubPath/etc//Content/site.css"

You can:

Simply change your Model.CssPath to a relative path (without leading '/') - ie "Content/site.css"

or

always use the equivalent of System.IO.Path.Combine when combining paths:

string path = new Uri(Url.Content("~"), Model.CssPath).ToString();
Sign up to request clarification or add additional context in comments.

2 Comments

You nailed it. Running my code behaves entirely different when deployed to a server using more paths. After removing the leading '/', and including Url.Content("~"), it works flawlessly. However, using the Path.Combine method should eliminate the problem even with incorrect slashes. Thank you.
Worth reviewing the comments against this post as new Uri may not behave exactly like Path.Combine
0

Change it to:

<link href="@path" rel="stylesheet" type="text/css" />

Using @path tell the Razor viewengine parser to pick out the variable path from the Razor server-side code context, and dump it's value into the HTML markup.

EDIT:

Interesting. I have the following in a view:

@{
    string path = Url.Content("~") + "Site.css";
}

<link href="@path" rel="stylesheet" type="text/css" />

The renderd HTML looks like this:

<link href="/Content/site.css" rel="stylesheet"/>

I'm not sure how // is appearing in your url.

EDIT:

Based on your comment, it sounds like Model.CssPath contains /Content/ which causes Url.Content to get into a mess, and that you simply need to just do:

href="@Model.CssPath"

1 Comment

I tried that, but it still doesn't resolve the domain in the url... the page's source still says: <link href="//Content/site.css" rel="stylesheet" type="text/css" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.