asp.net mvc3.0 razor 网站全站脚本、样式统一引用解决放方案

一、引言

在做网站是经常要对样式或者脚本进行更新,每次更新后需要客户端强制刷新才可以看到更新后的样式,或在样式文件应用处加版本号区分,

(常用的写法如“~/content/css/globe.css?v1.0”),还需要对每个引用 globe.css 文件的位置加版本号区分,工作量巨大。

所以,提供一个统一管理方案非常必要。


二、使用include.cofig 配置文件管理站点的所有 css 和 js 引用

命名规则:如果加入的是 css 文件 ,key 必须以 css_ 开头;如果是 js 文件 ,key 必须以 js_ 开头;

实例如下:

<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<appSettings>
<add key="css_css1" value="~/views/Center/css1.cshtml"/>
<add key="js_js1" value="~/views/Center/js1.cshtml"/>
</appSettings>
</configuration>


三、封装读取方法

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 对象扩展类
  /// </summary>
  public static class WebExtension
  {
    /// <summary>
    /// 导入Css文件
    /// </summary>
    /// <param name="url"></param>
    /// <param name="cssName">样式名</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());
    }
    /// <summary>
    /// 导入Js文件
    /// </summary>
    /// <param name="url"></param>
    /// <param name="cssName">脚本文件名</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(GetIncludeSettingValue("js_" + js))));
      }
      return new MvcHtmlString(str.ToString());
    }


    /// <summary>
    /// 获取自定义 Include.config 文件中的 appsetting 节点值
    /// </summary>
    /// <param name="key">节点名称</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.Config 配置文件:{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 配置文件,缺少必要的配置节 {0}", key));
      return config.AppSettings.Settings[key].Value;
    }
  }
}


四、页面调用

注意:需要引用扩展方法的命名空间

@using DiscussCenter.Ctrl.Utility;


  @Url.IncludeCss("css1", "css100")   //引用样式文件

  @Url.IncludeJs("js1")  //引用 脚本文件


草青工作室:http://blog.csdn.net/xxj_jing

原文地址:(http://blog.csdn.net/xxj_jing/article/details/8556780)


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值