如何在CSHTML文件中引用CSS? [英] how to refere CSS in CSHTML file?

查看:115
本文介绍了如何在CSHTML文件中引用CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Razor内容/子页面中应用/放置外部或额外的< script> 引用和CSS < link> ?

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> "

推荐答案

在_Layout中,您可以有2个部分:一个用于脚本,一个用于样式:

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>
}

这篇关于如何在CSHTML文件中引用CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆