ASP.NET MVC剃刀和CSS块

igo*_*GIS 2 css asp.net-mvc razor

我希望CSS样式块具有一定的灵活性,所以我决定注入一些服务器端代码,例如

#defaultGameContainer {
            position: relative;
            width: @(SiteConfig.Instance.Game.Width + "px");
            height: 600px;
            top: 100px;
            left: 50%;
            margin-left: -480px;
        }
Run Code Online (Sandbox Code Playgroud)

但是它似乎不起作用,我在VS中出现错误,例如“意外字符序列...”,我们是否限于在CSS中使用Razor语法?谢谢。

Mah*_*tib 5

实际上,您可以按照以下说明CSS为特定.cshtml视图添加自定义块:

首先,在.cshtml视图中定义您的自定义CSS块

@section CustomCSS{ 
<style>
    #floorsList > tbody > tr > td {
        vertical-align: middle
    }
</style>
}
Run Code Online (Sandbox Code Playgroud)

然后,你需要嵌入这个CustomCSS _layout.cshtml文件中:

<!DOCTYPE html>
<html>
 <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - Your Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @RenderSection("Styles", false)
    <!--Consider the following line -->
    @RenderSection("CustomCSS", required: false)
 </head>
 <body>
 </body>
</html>
Run Code Online (Sandbox Code Playgroud)