MVC3 use of helper syntax

  In ASP.NET WebSite, we write the general public App_Code method to achieve a class encapsulation and multiplexing. MVC3 in the presence of helper syntax can achieve the same purpose.
  Trying to look under Share Folder View _Layout.cshtml:
 
 
    
< head >
< title > @ViewBag.Title </ title >
< link href = " @Url.Content( " ~/ Content / Site.css " ) " rel = " stylesheet " type = " text/css " />
< script src = " @Url.Content( " ~/ Scripts / jquery - 1.5 . 1 .min.js " ) " type = " text/javascript " ></ script >
</ head >
     
      Css head portion of a reference file and Script, wherein Url.Content () relative path into the absolute path of the application, and the Server.MapPath () is similar. Use helper syntax is as follows:
 
    
@Css( " Site.css " )
@Script(
" jquery-1.5.1.min.js " )
@helper Script(
string scriptName)
{
< script src = " @Url.Content( " ~/ Scripts / " + scriptName) " type = " text/javascript " ></ script >
}
@helper Css(
string cssName)
{
< link href = " @Url.Content( " ~/ Content / " +cssName) " rel = " stylesheet " type = " text/css " />
}
       Under the new app_code project folder, create a new view file Content.cshtml in app_code, remove the inside of the Code, and the helper part of the copy of the original Layout.cshtml
To the next Content.cshtml, which is found Url is missing the referenced statement. Add @using System.Web.Mvc, and add a helper of parameters, as follows:
  
@using System.Web.Mvc
@helper Script(string scriptName,UrlHelper Url)
    {
    <script src="@Url.Content("~/Scripts/" + scriptName)" type="text/javascript"></script>
}
@helper Css(string cssName, UrlHelper Url)
    {
    <link href="@Url.Content("~/Content/" + cssName)" rel="stylesheet" type="text/css" />
}
 
 Layout.cshtml page so you can do this to Css and Scrpt quoted (default script into the Script folder, put Content style file folder)
<head>
    <title>@ViewBag.Title</title>
    @Content.Css("Site.css", Url)
    @Content.Script("jquery-1.5.1.min.js", Url)
</head>
 
 
 
 

Reproduced in: https: //www.cnblogs.com/orangepoet/archive/2011/07/05/2098561.html

Guess you like

Origin blog.csdn.net/weixin_34297704/article/details/93741379