gpt4 book ai didi

css - 更改样式表的 MVC URL/路由

转载 作者:行者123 更新时间:2023-11-28 11:08:18 25 4
gpt4 key购买 nike

我有一个为多个客户创建的应用程序。客户端之间的唯一区别是样式表和一些图形。我想让每个客户端只使用不同的 URL 来显示具有不同 css 的核心内容,如下所示:

我的应用程序/客户端1/我的 Controller /我的 Action
我的应用程序/客户端2/我的 Controller /我的 Action

谁能给我一些关于如何在 .NET MVC 4 中完成 is 的指示?

附加信息:
我只部署此应用程序的单个实例。我希望应用程序根据我的 url 的“客户端”部分动态使用正确的样式。

例子。 client1 的背景为绿色,client2 的背景为蓝色。除此之外,client1 和 client2 正在运行相同的 Web 应用程序。

我的 RouteConfig.cs 文件中有以下内容:

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "client1",
url: "client1/{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
name: "client2",
url: "client2/{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

更新解决方案:

这是我一直在寻找的答案:

    routes.MapRoute(
name: "client",
url: "{client}/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { client = "client1|client2" }
);

routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

最佳答案

注意:未经测试,只是一个想法。无法在评论中设置格式

您可以使用客户端 url 命名您的 css 文件:

test.com.styles.css

编辑

我刚刚意识到我们无法访问 BundleConfig.cs 中的 HttpContext,因为此时尚未创建 session 。

因此在您查看 (_Layout.cshtml) 时,您将生成如下样式表名称:

        @{
var host = HttpContext.Current.Request.Url.AbsoluteUri;
// Will be http://www.test.com/AREA1


var area = host.Split('/')[3];

// SPLIT will give you AREA1

var styleSheet = string.Format("{0}.styles.css", area);

// This will give you Stylesheet Name:
// So then you can use simple HTML to load the css file

var styleSheetUrl = string.Format("{0}{1}", "/LocationOfCssFile/", styleSheet);

}

<link rel="stylesheet" href="@styleSheetUrl">

您可以从这里获取您需要的 URL 的正确部分:

https://stackoverflow.com/a/593715/1910735

关于css - 更改样式表的 MVC URL/路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28676137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com