John
John

Reputation: 3402

Error using URL.Content in ASP.Net MVC

I have the following ASP.NET MVC page written in razor:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>SelectorTests</title>
    <link href="@{ Url.Content("~/Content/themes/base/jquery-ui.css"); }" rel="stylesheet" type="text/css" />
    <script src="@{ Url.Content("~/Scripts/jquery-1.4.4.min.js"); }" type="text/javascript"></script>
    <script src="@{ Url.Content("~/Scripts/jquery-ui.min.js"); }" type="text/javascript"></script>

    <script type="text/javascript">
        $(function () {
            $('h1').css('background-color', 'purple');
        });        
    </script>
</head>
<body>
    <div>
        <h1>My Header</h1>
        foobar
    </div>
</body>
</html>

URL.Content isn't working properly. When I do a View Source in FF, the relevant lines come back as

<link href="" rel="stylesheet" type="text/css" />
<script src="" type="text/javascript"></script>
<script src="" type="text/javascript"></script>

Please assist.

Upvotes: 2

Views: 3371

Answers (2)

Bala R
Bala R

Reputation: 108947

Try the following

<link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>

Upvotes: 1

kim3er
kim3er

Reputation: 6466

You're using curly braces when there is no need to.

<link href="@{ Url.Content("~/Content/themes/base/jquery-ui.css"); }" rel="stylesheet" type="text/css" />

Should be:

<link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />

You need only wrap code in curly braces when there is no return value. For example if you were declaring a variable. You would not need curly braces to then output the contents of the variable.

@{
    var foo = "bar";
}
<p>@foo</p>

Rich

Upvotes: 8

Related Questions