如何在ASP.NET中提取样式或Javascript包的输出
我正在开发一个在.cshtml文件中使用CSS包的单页应用程序,如下所示:如何在ASP.NET中提取样式或Javascript包的输出,asp.net,css,asp.net-mvc,bundling-and-minification,Asp.net,Css,Asp.net Mvc,Bundling And Minification,我正在开发一个在.cshtml文件中使用CSS包的单页应用程序,如下所示: public static MvcHtmlString GetInternalCss(this HtmlHelper html, string styleBundleVirtualPath) { //TODO: var bundledCss = "How do I extract the css from the virtual path?"; var i
public static MvcHtmlString GetInternalCss(this HtmlHelper html, string styleBundleVirtualPath)
{
//TODO:
var bundledCss = "How do I extract the css from the virtual path?";
var internalCss = string.Format("<style>{0}</style>", bundledCss);
return new MvcHtmlString(internalCss);
}
@style.Render(“~/content/css/bundle-css”)
但是,为了提高站点的加载性能,我想将捆绑和缩小的整个CSS嵌入到我的.cshtml文件中。因此,我想将上面的代码转换为:
@Html.GetInternalCss(“~/content/css/bundle-css”)
其中GetInternalCss是一个扩展方法,它将采用css虚拟路径并输出内部css。代码如下所示:
public static MvcHtmlString GetInternalCss(this HtmlHelper html, string styleBundleVirtualPath)
{
//TODO:
var bundledCss = "How do I extract the css from the virtual path?";
var internalCss = string.Format("<style>{0}</style>", bundledCss);
return new MvcHtmlString(internalCss);
}
public static MvcHtmlString GetInternalCss(此HtmlHelper html,字符串样式bundleVirtualPath)
{
//待办事项:
var bundledCss=“如何从虚拟路径提取css?”;
var internalCss=string.Format(“{0}”,bundledCss);
返回新的MvcHtmlString(internalCss);
}
我知道捆绑的css在内存中的某个地方。我只是想知道怎么把它弄出来。谢谢你的帮助。我可能已经用下面的代码回答了我自己的问题:
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;
using System.Web.Mvc;
using System.Web.Optimization;
namespace SomeNameSpace
{
public static class HtmlHelpExtensions
{
private static readonly IDictionary<string,string> InternalCssCache = new ConcurrentDictionary<string, string>();
public static MvcHtmlString GetInternalCss(this HtmlHelper html, string styleBundleVirtualPath)
{
string internalCss = null;
if (InternalCssCache.TryGetValue(styleBundleVirtualPath, out internalCss))
return new MvcHtmlString(internalCss);
//download the css
var relativeUrl = Styles.Url(styleBundleVirtualPath);
var url = html.ViewContext.HttpContext.Request.Url + relativeUrl.ToString().Trim('/');
var webClient = new WebClient();
var css = webClient.DownloadString(url);
internalCss = string.Format("<style>{0}</style>", css);
InternalCssCache[styleBundleVirtualPath] = internalCss;
return new MvcHtmlString(internalCss);
}
}
}
使用System.Collections.Concurrent;
使用System.Collections.Generic;
Net系统;
使用System.Web.Mvc;
使用System.Web.Optimization;
名称空间名称空间
{
公共静态类HtmlHelpExtensions
{
私有静态只读IDictionary InternalCssCache=新ConcurrentDictionary();
公共静态MvcHtmlString GetInternalCss(此HtmlHelper html,字符串样式BundleVirtualPath)
{
字符串内部css=null;
if(InternalCssCache.TryGetValue(styleBundleVirtualPath,out internalCss))
返回新的MvcHtmlString(internalCss);
//下载css
var relativeUrl=Styles.Url(styleBundleVirtualPath);
var url=html.ViewContext.HttpContext.Request.url+relativeUrl.ToString().Trim('/');
var webClient=新的webClient();
var css=webClient.DownloadString(url);
internalCss=string.Format(“{0}”,css);
InternalCssCache[styleBundleVirtualPath]=internalCss;
返回新的MvcHtmlString(internalCss);
}
}
}
您是否可以作为答案而不是编辑发布?