Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using URL.Content in ASP.Net MVC

Tags:

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.

like image 461
John Avatar asked Apr 27 '26 17:04

John


1 Answers

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

like image 69
kim3er Avatar answered Apr 29 '26 06:04

kim3er



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!