`

asp.net mvc处理css和js版本问题

MVC 
阅读更多

       当服务的修改了js和css内容后,发布到IIS服务器上,总是导致客户端内容显示不正确,原因是客户端存在缓存,还是加载的原来的js和css问题。

  在css或js后面添加版本号,例如:

<script src="1.js?v=1.0"></script>

 当版本号改变时,客户端会重新读取新的文件,问题解决了。

  但同时也带了另一个问题,每次修改后,都必须修改版本好,如果引用比较多的化,那么工作量也比较大,同样是个问题。

       这里解决方法是配置一个版本号

  步骤一:在web.config中配置版本号:

 <appSettings>
    <add key="JsVersion" value="20161123.2"/>
  </appSettings>

 步骤二:写一个扩展方法

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebSeat.Site.Member.Helper
{
    /// <summary>
    /// 说明:
    /// 创建日期:2016/11/23 10:36:01
    /// 创建人:曹永承
    /// </summary>
    public static class HtmlHelperExtend
    {
        /// <summary>
        /// 给CSS文件或JS文件指定版本号
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="url">CSS或JS路径</param>
        /// <returns></returns>
        public static string GetCssJsUrl(this HtmlHelper helper, string url)
        {
            string version = ConfigurationManager.AppSettings["JsVersion"];
            version =version == null ? "1.0" : version;
            return url += "?v=" + version;
        }
    }
}

 步骤三:改变js和css引用方法

<link href="@Html.GetCssJsUrl("/Content/custom/areaHome.css")" rel="stylesheet"/>
<script src="@Html.GetCssJsUrl("/Scripts/custom/AreaHome.js")"></script>

 

  可以看到css和js文件自动加上了版本号

分享到:
评论

相关推荐

    Pro ASP.NET MVC 5 epub

    The ASP.NET MVC 5 Framework is the latest evolution of Microsoft’s ASP.NET web platform. It provides a high-productivity programming model that promotes cleaner code architecture, test-driven ...

    精通ASP.NET MVC5

    ASP.NET MVC 5框架是微软ASP.NET Web平台的新进展。...本书还涉及流行的Bootstrap JavaScript库,该库现已被纳入到MVC 5之中,为开发人员提供更广泛的多平台CSS和HTML 5选项,而不必像以前那样去加载大量的第三方库。

    Bootstrap.for.ASP.NET.MVC.2nd.Edition.17858894

    Combine the power of ASP.Net MVC 6 with Bootstrap 4 to build elegant, responsive web apps About This Book Updated for Bootstrap 4 and ASP.Net MVC 6, this book shows how to take advantage of the ...

    Asp.net MVC5 手机Web应用开发(高清英文pdf)

    Mobile ASP.NET MVC 5 will take you step-by-step through the process of developing fluid content that adapts its layout to the client device using HTML, JavaScript and CSS, and responsive web design....

    asp.net Mvc开发的资产管理系统.zip

    用的是ASP.NET MVC开发模式,可以导入和导出excel表格,使用了js、ajax,html,css后端使用nhibernate连接数据库,数据库用的sqlsever2015,系统主要有3个模块,部门、用户和资产模块,各模块都有多级分页模糊查询,...

    精通 ASP.NET MVC 5 (Adam Freeman) [中文版]高清完整PDF版

    ASP.NET MVC 5框架是微软ASP.NET Web平台的新进展。它提供了高生产率的编程模型,结合ASP.NET的全部优势,促成更整洁的代码架构、测试驱动开发和强大的可扩展性。 本书涵盖ASP.NET MVC 5的所有开发优势技术,包括用...

    精通ASP.NET MVC5.rar

    ASP.NET MVC 5框架是微软ASP.NET Web平台的新进展。它提供了高生产率的编程模型,结合ASP.NET的全部优势,促成更整洁的代码架构、测试驱动开发和强大的可扩展性。 本书涵盖ASP.NET MVC 5的所有开发优势技术,包括用...

    Bootstrap for ASP.NET MVC, 2nd Edition

    Learn to use the various Bootstrap CSS and HTML elements, and how to use the new Bootstrap 4 grid layout system with ASP.Net MVC Explore the different input groups and implement alerts, progress bars,...

    BForms, 用于 ASP.NET MVC的Bootstrap 表单.zip

    BForms, 用于 ASP.NET MVC的Bootstrap 表单 Bootstrap 支持 ASP.NET MVC控件BForms开源框架由一个由 ASP.NET MVC 。Javascript 。Javascript和自定义CSS组成的Collection 框架组成,它扩展了 Twitter

    ASP.NET MVC网上图书商城电商网站运营源码

    2项目使用 ASP.NET MVC框架,使用ADO.NET实体数据模型 EF连接数据库,view视图用Razor视图,控制器动作方法使用 Linq to sql 实现与数据库进行数据交互,并完成业务逻辑操作 3页面使用伪静态处理 网站前台 1Web前端...

    Pro ASP.NET MVC 5.mobi

    The ASP.NET MVC 5 Framework is the latest evolution of Microsoft’s ASP.NET web platform. It provides a high-productivity programming model that promotes cleaner code architecture, test-driven ...

    ASP.NET MVC+EF6+Bootstrap开发C#源代码

    一套基于ASP.NET MVC+EF6+Bootstrap开发出来的框架源代码! 采用主流框架,容易上手,简单易学,学习成本低。可完全实现二次开发、基本满足80%项目需求。 可以帮助解决.NET项目70%的重复工作,让开发更多关注业务...

    Pro ASP.NET MVC 5.pdf

    The ASP.NET MVC 5 Framework is the latest evolution of Microsoft’s ASP.NET web platform. It provides a high-productivity programming model that promotes cleaner code architecture, test-driven ...

    ASP.NET MVC框架精美Demo网站

    演示了ASP.NET框架MVC模式,包括路由,SQL Server数据库操作,LINQ,CSS美化,JavaScript动画等等

    mvc js和css文件压缩合并

    演示asp.net mvc4下通过BundleConfig对js和css进行压缩和并,提高网页响应速度,优化网站

    MVC2.0入门必读教程程序源码

    为了将大家的关注点充分集中在ASP.NET MVC上,这个Demo的业务处理将使用Mock的方式。即不会真正去访问数据库,而是虚拟一些数据。 2.本Demo将不考虑任何美工问题。 下面,让我们一起开始ASP.NET MVC之旅吧。 ...

    ASP.NET MVC 5 Mobile

    Mobile ASP.NET MVC 5 will take you step-by-step through the process of developing fluid content that adapts its layout to the client device using HTML, JavaScript and CSS, and responsive web design....

    部署到iis后无法加载运行CSS文件的解决方法

    如何在IIS环境下部署httpswin2008 IIS7无后缀URL部署问题 MVC4 MVC URL映射windows2003 IIS6 部署MVC3和MVC4程序的方法IIS 7.5 部署ASP.NET失败的解决方法IIS部署asp.net报404错误的解决方法win2003 sp2+iis 6.0上...

    精通asp.net mvc5

    ASP.NET MVC 5框架是微软ASP.NET Web平台的新进展。它提供了高生产率的编程模型,结合ASP.NET的全部优势,促成更整洁的代码架构、测试驱动开发和强大的可扩展性。 本书涵盖ASP.NET MVC 5的所有开发优势技术,包括用...

Global site tag (gtag.js) - Google Analytics