简体   繁体   English

如何从C#MVC4中的插件更改资源(JS和CSS)URL

[英]How to Change Resources (JS & CSS) URL from Plugin in C# MVC4

I'm working on nopCommerce and noticed that scripts and css are included in following way, in the Root_Head.cshtml file 我正在使用nopCommerce,注意到Root_Head.cshtml文件中以以下方式包含了脚本和CSS。

Html.AppendCssFileParts("~/Content/smoothness/jquery-ui-1.8.17.custom.css"); 
Html.AppendScriptParts("~/Scripts/public.ajaxcart.js");
Html.AppendScriptParts("~/Scripts/public.common.js");
Html.AppendScriptParts("~/Scripts/jquery-ui.min.js");
Html.AppendScriptParts("~/Scripts/jquery.validate.unobtrusive.min.js");
Html.AppendScriptParts("~/Scripts/jquery.validate.min.js");
Html.AppendScriptParts("~/Scripts/jquery.unobtrusive-ajax.min.js");
Html.AppendScriptParts("~/Scripts/jquery-1.7.1.min.js");

Late in the same document, it is embedded in the Head: 在同一文档的后期,它嵌入在Head中:

@Html.NopCssFiles(this.Url, ResourceLocation.Head)
@Html.NopScripts(this.Url, ResourceLocation.Head)

This generates the html output to embed the scripts / css in header / footer as defined with relative URL like... 这将生成html输出,以将脚本/ css嵌入标头/页脚中,并使用相对URL进行定义,例如...

<link href="/Content/smoothness/jquery-ui-1.8.17.custom.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.7.1.min.js"></script>

I need to change the URL to other URL from plugin without modifying root files. 我需要将URL从插件更改为其他URL,而无需修改根文件。 So after this is done, it will look like this: 因此,完成此操作后,它将如下所示:

<link href="http://someurl.com/Content/smoothness/jquery-ui-1.8.17.custom.css" rel="stylesheet" type="text/css" />
<script src="http://someurl.com/Scripts/jquery-1.7.1.min.js"></script>

Any idea on how can I extend this from plugin? 关于如何从插件扩展它的任何想法? How should I proceed? 我应该如何进行?

I'm not sure how nopCommerce works but if your using MVC4, then you can use a new feature call Bundling and Minification 我不确定nopCommerce的工作方式,但是如果您使用的是MVC4,则可以使用新功能“捆绑和缩小”

One big advantage is, this will combine all the js and css into one file and do minification, so it will improve the performance of your website 一个很大的好处是,这将把所有的js和css合并到一个文件中并进行缩小,因此将提高您网站的性能

ref: http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification 参考: http : //www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

inside the BundleConfig.cs, you can do something like this 在BundleConfig.cs中,您可以执行以下操作

        var domainUrl = "http://someurl.com"; // you can retrieve it from config file

        bundles.Add(new StyleBundle(domainUrl + "/Content/css").Include( domainUrl + "/Content/site.css"));


        bundles.Add(new StyleBundle(domainUrl + "/Content/themes/base/css").Include(
                    domainUrl + "/Content/themes/base/jquery.ui.core.css",
                    domainUrl + "/Content/themes/base/jquery.ui.resizable.css",
                    domainUrl + "/Content/themes/base/jquery.ui.selectable.css",
                    domainUrl + "/Content/themes/base/jquery.ui.accordion.css",
                    domainUrl + "/Content/themes/base/jquery.ui.autocomplete.css",
                    domainUrl + "/Content/themes/base/jquery.ui.button.css",
                    domainUrl + "/Content/themes/base/jquery.ui.dialog.css",
                    domainUrl + "/Content/themes/base/jquery.ui.slider.css",
                    domainUrl + "/Content/themes/base/jquery.ui.tabs.css",
                    domainUrl + "/Content/themes/base/jquery.ui.datepicker.css",
                    domainUrl + "/Content/themes/base/jquery.ui.progressbar.css",
                    domainUrl + "/Content/themes/base/jquery.ui.theme.css"));

        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

Then in the cshtml, you can use following method 然后在cshtml中,可以使用以下方法

@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/jquery")

* EDIT * * 编辑*

Sorry i just re-looked at the answer, and i could of just done this 抱歉,我只是重新查看了答案,我可以这样做

in your web.config 在您的web.config中

<configuration>
    <appSettings>
        <add key="resourcePath" value="http://someurl.com"/>
    </appSettings>
 . . .
</configuration>

then in your html 然后在你的HTML

var url = ConfigurationManager.AppSettings["resourcePath"];
Html.AppendCssFileParts( url + "/Content/smoothness/jquery-ui-1.8.17.custom.css"); 
Html.AppendScriptParts(url + "/Scripts/public.ajaxcart.js");
Html.AppendScriptParts(url + "/Scripts/public.common.js");
Html.AppendScriptParts(url + "/Scripts/jquery-ui.min.js");
Html.AppendScriptParts(url + "/Scripts/jquery.validate.unobtrusive.min.js");
Html.AppendScriptParts(url + "/Scripts/jquery.validate.min.js");
Html.AppendScriptParts(url + "/Scripts/jquery.unobtrusive-ajax.min.js");
Html.AppendScriptParts(url + "/Scripts/jquery-1.7.1.min.js");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM