Combining CSS files depending on the request domain?

I have an application with several tenants, and I'm trying to determine the simplest ways to manage CSS files depending on the URL of any incoming request.

I think I can have some conditional logic inside RegisterBundles () that takes Url as a string and binds accordingly:

public static void RegisterBundles(BundleCollection bundles, string tenant = null) {
     if (tenant == "contoso"){
           bundles.Add(new StyleBundle("~/contoso.css") 
     }
}

But I do not know how to pass a string to RegisterBundles, even if it is possible, or the correct solution. Any help here would be amazing.

+4
source share
3 answers

RegisterBundles . ASP.net -CSS ( HttpContext.Cache).

, RegisterBundles, .

:

@Styles.Render("~/Content/" + ViewBag.TenantName)

Edit:

, TenantName ViewBag , . - Styles.Render(), , .

public static class TenantStyles
{
    public static IHtmlString Render(params string[] paths)
    {
        var tenantName = "test"; //get tenant name from where its currently stored
        var tenantExtension = "-" + tenantName;
        return Styles.Render(paths.Select(i => i + tenantExtension).ToArray());
    }
}

@TenantStyles.Render("~/Content/css")

{bundle} - {tenant}, ~/Content/css-test. .

+6

, , BundleCollection. , . / . ASP.NET , VirtualPathProvider. .

. SO.

+1

, , CSS URL- , css .

  • : ResourceController

    // CREATE PATH TO CSS FOLDER, I store in webconfig     <add key="PathToStyles" value="/Content/MyTheme/" />
    private static string _pathToStyles = ConfigurationManager.AppSettings["PathToStyles"];
    
    public void Script(string resourceName)
    {
        if (!String.IsNullOrEmpty(resourceName))
        {
            var pathToResource = Server.MapPath(Path.Combine(_pathToScripts, resourceName));
    
            TransmitFileWithHttpCachePolicy(pathToResource, ContentType.JavaScript.GetEnumDescription());
        }
    }
    
    public void Style(string resourceName)
    {
        if (!String.IsNullOrEmpty(resourceName))
        {
            var pathToResource = Server.MapPath(Path.Combine(_pathToStyles, resourceName));
    
            TransmitFileWithHttpCachePolicy(pathToResource, ContentType.Css.GetEnumDescription());
        }
    }
    
    private void TransmitFileWithHttpCachePolicy(string pathToResource, string contentType)
    {
        //DO WHAT YOU WANT HERE;
        Response.ContentType = contentType;
        Response.TransmitFile(pathToResource);
    }
    
    //You can handle css or js file...
    private enum ContentType
    {
        [EnumDescription("text/css")]
        Css,
        [EnumDescription("text/javascript")]
        JavaScript
    }
    
  • Global.asax.cs , start medthod,

    protected void Application_Start()
    {
         RouteConfig.RegisterRoutes(RouteTable.Routes);
    
    }
    
  • routeConfig, ( ):

      routes.MapRoute(
            name: "Resource",
            url: "resource/{action}/{resourceName}",
            defaults: new { controller = "Resource" }
        );
    
  • UrlHelperExtensions, webconfig

    public static class UrlHelperExtensions
    {
        public static string Style(this UrlHelper urlHelper, string resourceName)
        {
        return urlHelper.Content(String.Format("~/resource/style/{0}", resourceName));
        }
     }
    
  • css , :

... "<" link href= "@Url.Style(" yourcss.css ")" rel= "stylesheet" type = "text/css" /" >

  • .
0

All Articles