Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to refere CSS in CSHTML file?

How can I apply/put external or extra <script> references and CSS <link> in my Razor content/child pages?

@model Portal.Common.Models.TempModel
@{   

    ViewBag.Title = "asdasd";
    ViewBag.MissionId = id;
    ViewBag.id = 0;
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@if (false)
{
    <script src="../../Scripts/jquery-1.7.1-vsdoc.js" type="text/javascript"></script>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
    <script src="...../ui/jquery.ui.mouse.js"></script>
    <script src="...../ui/jquery.ui.draggable.js"></script>         
}
<style type="text/css">
img[src*="iws3.png"] {
    display: none;
}
@section JavaScript {
<script type="text/javascript" src="https://www.google.com/jsapi"></script>    
<script type="text/javascript"></script> "
like image 781
user1802212 Avatar asked Feb 18 '23 16:02

user1802212


1 Answers

In your _Layout you could have 2 sections: one for the scripts and one for the styles:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    @RenderSection("Styles", false)
</script>
</head>
<body>
    @RenderBody()
    @RenderSection("Scripts", false)
</body>
</html>

and then in your view override the sections you want:

@model Portal.Common.Models.TempModel 

@{
    ViewBag.Title = "asdasd";
    ViewBag.MissionId = id;
    ViewBag.id = 0;
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@section Styles {
    <!-- main CSS -->
    <link href="@Url.Content("~/Content/Site.css)" rel="stylesheet" type="text/css" />

    <!-- some external CSS -->
    <link href="http://www.example.com/foo.css" rel="stylesheet" type="text/css" />

    <!-- some inline CSS -->
    <style type="text/css">
        img[src*="iws3.png"] {
            display: none;
        }
    </style>
}
@section Scripts {
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>    
    <script src="@Url.Content("~/ui/jquery.ui.mouse.js")></script>
    <script src="@Url.Content("~/ui/jquery.ui.draggable.js")></script>
    <script type="text/javascript">
        // some inline javascript
    </script>
}
like image 171
Darin Dimitrov Avatar answered Mar 09 '23 10:03

Darin Dimitrov