Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过会话或cookie动态呈现样式_C#_Asp.net Mvc_Session_Cookies - Fatal编程技术网

C# 如何通过会话或cookie动态呈现样式

C# 如何通过会话或cookie动态呈现样式,c#,asp.net-mvc,session,cookies,C#,Asp.net Mvc,Session,Cookies,我已经为我的样式表定义了不同的包。 我试着这样做:在\u Layout.cshtml @if (Session["UserId"] == null) { Styles.Render("~/Content/Darkly"); } else { var User = Model.Single(x => x.UserId.ToString() == Session["UserId"].ToString()); Styles.Render(string.Format("~/

我已经为我的样式表定义了不同的包。 我试着这样做:在
\u Layout.cshtml

@if (Session["UserId"] == null)
{
    Styles.Render("~/Content/Darkly");
}
else
{
    var User = Model.Single(x => x.UserId.ToString() == Session["UserId"].ToString());
    Styles.Render(string.Format("~/Content/{0}", User.Settings.Style));
}
但出于某些原因,这并不奏效。 还有其他方法可以做到这一点吗

这是
BundleConfig.cs

`


`

我想我知道这里出了什么问题,你应该在渲染之前添加“@”符号

像这样:

@if (Session["UserId"] == null)
{
    @Styles.Render("~/Content/css");
}
else
{
    @Styles.Render(string.Format("~/Content/{0}", "Darkcss"));
}

这实际上将使Razor能够渲染渲染方法的输出。

能否共享BundleConfig.cs?我怀疑你没有在你的应用程序中配置那些特定于用户的样式。如果UserId等于“Foo”,你必须有如下捆绑配置:
cs bundles.Add(new StyleBundle(“~/Content/Foo”)。包括(…)
关于如何配置捆绑包,请参阅此部分。不,我不这么认为。我使用用户ID查找用户,然后使用该用户首选项设置捆绑包。
用户设置。样式
包含捆绑包的最后一部分虚拟路径BundleConfig看起来正确。我在我的机器上尝试了它,它可以正常工作。检查以下内容:1。css文件的顺序2.样式的名称实际上与捆绑包中的名称匹配这些名称确实匹配。但是为什么css文件的顺序?以及在代码(捆绑包)中的位置?这应该可以工作,但是现在我在这一行中得到了错误
var User=Model.Single(x=>x.UserId.ToString()==Session[“UserId”].ToString();
LINQ to Entities无法识别方法'System.Object get_Item(System.String)'方法,但登录时几乎是在my AccountController中。请尝试在LINQ语句之前的变量中提取“Session[“UserId”].ToString()”,并在LINQ本身中使用字符串变量。var idFromSession=Session[“UserId”].ToString();Model.Single(x=>x.UserId.ToString()==idFromSession),但我建议避免从视图中查询DB。最好在controller中提取用户并在HttpContext上设置CurrentUser。
@if (Session["UserId"] == null)
{
    @Styles.Render("~/Content/css");
}
else
{
    @Styles.Render(string.Format("~/Content/{0}", "Darkcss"));
}