Asp.net mvc3.0 razor Website full-site scripting and style reference solution

Source: Internet
Author: User

I. Introduction when creating a website, you often need to update styles or scripts. After each update, You need to force the client to refresh the updated styles, or add version numbers to the style file application to differentiate them, (For example, "~ /Content/css/globe.css? V1.0), you also need to add version numbers to each location that references the globe.css file, which requires a huge workload. Therefore, it is necessary to provide a unified management solution. Ii. Use include. cofig configuration file management all css and js reference naming rules of the site: If a css file is added, the key must start with css _; if it is a js file, the key must start with js; example: <? Xml version = "1.0"?> <Configuration> <system. web> <compilation debug = "true" targetFramework = "4.0"/> </system. web> <deleetask> <add key = "css_css1" value = "~ /Views/Center/css1.cshtml "/> <add key =" js_js1 "value = "~ /Views/Center/js1.cshtml "/> </appSettings> </configuration> 3. encapsulate the reading method using System; using System. configuration; using System. IO; using System. text; using System. web; using System. web. mvc; using System. linq; namespace DiscussCenter. ctrl. utility {// <summary> /// Html object extension class /// </summary> public static class WebExtension {/// <summary> /// import the Css file/ /// </summary> /// <param name = "url"> </param> /// <param nam E = "cssName"> style name </param> // <returns> </returns> public static MvcHtmlString IncludeCss (this UrlHelper url, params string [] cssName) {StringBuilder str = new StringBuilder (); foreach (string css in cssName) {str. append (string. format ("<link type = \" text/css \ "rel = \" stylesheet \ "href = \" {0} \ "/>", url. content (GetIncludeSettingValue ("css _" + css);} return new MvcHtmlString (str. toString ();} // <su Mmary> /// import the Js file /// </summary> /// <param name = "url"> </param> /// <param name = "cssName"> script file name </param> /// <returns> </returns> public static MvcHtmlString IncludeJs (this UrlHelper url, params string [] jsName) {StringBuilder str = new StringBuilder (); foreach (string js in jsName) {str. append (string. format ("<script type = \" text/javascript \ "src = \" {0 }\ "> </script>", url. content (GetIncludeSettingV Alue ("js _" + js);} return new MvcHtmlString (str. toString ();} // <summary> // get custom Include. the value of the deleetting node in the config file /// </summary> www.2cto.com // <param name = "key"> node name </param> /// <returns> </returns> public static string GetIncludeSettingValue (string key) {string indexConfigPath = Path. combine (HttpContext. current. request. physicalApplicationPath, "Include. config "); if (! File. exists (indexConfigPath) throw new Exception (string. format ("Include. Missing. config configuration file: {0} ", indexConfigPath); ExeConfigurationFileMap ecf = new ExeConfigurationFileMap (); ecf. exeConfigFilename = indexConfigPath; Configuration config = ConfigurationManager. openMappedExeConfiguration (ecf, ConfigurationUserLevel. none); if (! Config. appSettings. settings. allKeys. contains (key) throw new Exception (string. format ("Include. config configuration file, the required configuration section {0} ", key) is missing); return config. appSettings. settings [key]. value ;}}4. Page call Note: The namespace of the extension method to be referenced @ using DiscussCenter. ctrl. utility; @ Url. includeCss ("css1", "css100") // reference the Style File @ Url. includeJs ("js1") // reference the script file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.