asp.net mvc3.0 razor 网站全站脚本、样式统一引用解决放方案
来源:互联网 发布:mac切换大小写灯不亮 编辑:程序博客网 时间:2024/06/06 09:55
一、引言
在做网站是经常要对样式或者脚本进行更新,每次更新后需要客户端强制刷新才可以看到更新后的样式,或在样式文件应用处加版本号区分,
(常用的写法如“~/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)
- asp.net mvc3.0 razor 网站全站脚本、样式统一引用解决放方案
- asp.net mvc3.0 razor 网站全站脚本、样式统一引用解决放方案
- asp.net网站中,全站统一设置title,keywords,description的实现方案
- ASP.NET MVC3 Razor 语法
- ASP.NET MVC3 Razor布局
- Asp.net MVC3 Razor语法
- asp.net mvc3 razor简介
- ASP.NET MVC3.0 中 Razor 母版嵌套
- ASP.NET MVC3 C# Razor 语法
- ASP.NET MVC3 Razor语法简介
- ASP.NET MVC3的Razor语法
- ASP.NET MVC3: Razor中的布局
- ASP.NET MVC3: Razor 中的布局
- ASP.NET MVC 3 Beta: Built-in support for charts(MVC3 Razor中使用图表的最佳方案)
- ASP.Net Mvc3 Razor +Sql Server Reporting Service
- ASP.NET MVC3 实例(五) Razor 布局简介
- ASP.NET MVC3 实例(五) Razor 布局简介
- 将 ASP.NET MVC3 Razor 项目部署到虚拟主机中
- WINDOWS下更改MYSQL数据路径(datadir)
- user管理
- 获取 GPG 密钥失败:[Errno 14] Could not open/read file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rpmfusion-free-fedora
- C++ 多线程编程总结
- datatables request unknown parameter '0' from data source for row 0
- asp.net mvc3.0 razor 网站全站脚本、样式统一引用解决放方案
- Windows多线程编程(2)
- 北京申请工作居住证
- Linux创建静态库和动态库
- Outlook 2010 收邮件word,excel附件打不开问题处理
- Eclipse3.7中搭建Android开发环境
- 非集群client访问HBase方法
- ubuntu 11.10 lightdm 循环在登陆界面
- 使用js解析xml文档和xml字符串(ie和火狐)