Как сделать CSS встроенным в веб-форме ASP.NET - PullRequest
0 голосов
/ 08 июня 2018

Я использую System.Web.Optimization для объединения CSS и скрипта. Но теперь я хочу сделать CSS и скрипты встроенными, а не ссылаться на один файл.

Я пытаюсь создать метод расширениядля System.Web.Optimization.Render () , но я не могу предоставить параметр как HttpContextBase для метода GenerateBundleResponse ()

Ниже приведен мой код с ошибкой

    public static class OptimizationStylesExtention
    {

      private static string GetBundleContent(HttpContextBase httpContextBase, 
        string bundleVirtualPath)
       {
            return BundleTable.Bundles
                .Single(b => b.Path == bundleVirtualPath)
                .GenerateBundleResponse(new BundleContext(httpContextBase, 
                            BundleTable.Bundles, bundleVirtualPath)).Content;
        }

    public static IHtmlString RenderInline(this HtmlString str, params string[] 
    bundleVirtualPath)
    {
        StringBuilder bundleContent = new StringBuilder();
        foreach (var virtualPath in bundleVirtualPath)
        {
            bundleContent.Append(BundleTable.Bundles.Single(b => b.Path == virtualPath)
            .GenerateBundleResponse(new BundleContext(str, BundleTable.Bundles, virtualPath)));
        }
        return new HtmlString(string.Format("{<style>{0}</style>}", bundleContent.ToString()));
    }
}

1 Ответ

0 голосов
/ 21 февраля 2019

Здравствуйте, вы можете сделать, используя GenerateBundleResponse Метод.Какой метод BundleResponse class.

Вот код

using System.Web.Optimization;

/// <summary>
/// This is the extension for the bundle. 
/// Which is used when we want to use inline css with style tag
/// </summary>
public static class BundleExtensions
{
    /// <summary>
    /// Returns inline css with style tag
    /// </summary>
    /// <param name="VirtualPath">Accepts Virtual Path</param>
    /// <returns>string as inline css</returns>
    public static string InlineStyle(string VirtualPath)
    {
        var CSSBundle = BundleTable.Bundles.GetBundleFor(VirtualPath);
        var CSS = CSSBundle.GenerateBundleResponse(new BundleContext(new System.Web.HttpContextWrapper(System.Web.HttpContext.Current), BundleTable.Bundles, string.Empty)).Content;
        return string.Format("<style type='text/css'>{0}</style>",CSS);
    }
    /// <summary>
    /// Returns inline script with script tag
    /// </summary>
    /// <param name="VirtualPath">Accepts Virtual Path</param>
    /// <returns>string as inline script</returns>
    public static string InlineScript(string VirtualPath)
    {
        var JSBundle = BundleTable.Bundles.GetBundleFor(VirtualPath);
        var JS = JSBundle.GenerateBundleResponse(new BundleContext(new System.Web.HttpContextWrapper(System.Web.HttpContext.Current), BundleTable.Bundles, string.Empty)).Content;
        return string.Format("<script type='text/javascript'>{0}</script>", JS);
    }
}

И на вашей странице ASP.NET этот метод можно вызвать следующим образом

<%=BundleExtensions.InlineStyle("~/css/new_home_page") %>

Спасибо:)

...