问题
I would like to do the following:
@Scripts.Render("~/bundles/jquery?version=1"])
the version value has to be dynamic and should match the value defined in a cookie.
How can I add this parameter to Scripts.Render ?
I've tried something like that with jQuery but with no luck:
@Scripts.Render("~/bundles/jquery?version=" + $.cookie('version'))
回答1:
Replace
@Scripts.Render("~/bundles/jquery?version=1"])
with
@{string version = 1}
@Scripts.RenderFormat("<script type=\"text/javascript\" src=\"{0}?nocache="+ version +"\"></script>", "~/bundles/jquery")
as shown in this post: http://www.jomendez.com/2016/05/26/how-to-avoid-js-files-cache-script-bundle-with-razor/
回答2:
By default MVC optimisations will automatically add a version parameter to the bundle links for release builds but not for debug. E.g. when you deploy your site, the link to /bundles/modernizr becomes something like /bundles/modernizr?v=inCVuEFe6J4Q07A0AcRsbJic and the JavaScript is minified.
If one of the files in the bundle has changed the parameter changes on next deployment, hence linked files are cached by browsers but reloaded from the server when they have changed in a new release.
For easier debugging optimisations are disabled in debug (= no version parameter added and no minified code). If you want to override this you can set the compilation debug attribute to false in web.config, or you can enable optimizations in code, like so:
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
// Code removed for clarity.
BundleTable.EnableOptimizations = true;
}
For full details see http://www.asp.net/mvc/overview/performance/bundling-and-minification, in particular the sections "Controlling Bundling and Minification" and "Bundle Caching".
回答3:
For all I know @Scripts.Render("~/bundles/jquery") is not a path. It's just name. So if you want to use different versions you should create two bundles in your BundleConfig:
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery-ver191").Include(
"~/Scripts/jquery-1.9.1js"));
bundles.Add(new ScriptBundle("~/bundles/jquery-ver202").Include(
"~/Scripts/jquery-2.0.2js"));
}
Than you can call necessary version in your view:
@Scripts.Render("~/bundles/jquery-ver191")
or
@Scripts.Render("~/bundles/jquery-ver202")
EDITED: On your comment: But this default code in BundleConfig is the same you want.
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
Or you don't have an oppurtunity to delete other version exept newest?
回答4:
Create a mvc helper that changes the html generated and adds the version. The follow example works when a single file or multiple files (debug mode) are generated.
public static HtmlString GetScriptsWithVersion()
{
const string VERSION = "2.0.1"; //or get the version where you want
const string SCRIPT_END = "\"></script>";
string html = Scripts.Render("~/bundles/ui").ToString();
string versionParam = "?v=" + VERSION
html = html.Replace(SCRIPT_END, versionParam + SCRIPT_END);
return new HtmlString(html);
}
来源:https://stackoverflow.com/questions/18075419/asp-net-mvc-4-adding-parameter-to-scripts-render-path