MVC3.0入门学习笔记Razor 之样式加载方式1

项目下添加App_Code 文件夹(手动添加重命名)

添加 Style.cshtml

基于 helper类 写通用方法

@using System.Web.Mvc;
@using WebMatrix.Data;//这个要手动拷贝到bin目录下 不知道为什么添加引用后不自动过去
@helper Script(string scriptName, UrlHelper Url) { <script src="@Url.Content(scriptName)" type="text/javascript"></script> } @helper Css(string cssName, UrlHelper Url) { <link href="@Url.Content(cssName)" rel="stylesheet" type="text/css" /> }

 然后其他.cshtml调用方法

@Style.Css("/offerstyle/css/offer-manage.css", this.Url)

@Style.Script("/script/formValidator.js", this.Url)

这样就不用每次都写

<script src="@Url.Content("xxxxxx")" type="text/javascript"></script>了
原文地址:https://www.cnblogs.com/jcgh/p/2385389.html