gpt4 book ai didi

asp.net-mvc-3 - Razor Engine - 如何根据不同条件呈现不同的 body 标签?

转载 作者:行者123 更新时间:2023-12-04 00:04:05 28 4
gpt4 key购买 nike

这是我目前拥有的代码:

@{
if (Request.Browser.Browser == "IE") {
if (Request.Browser.MajorVersion == 7) { <body class="ie7"> }
if (Request.Browser.MajorVersion == 8) { <body class="ie8"> }
if (Request.Browser.MajorVersion == 9) { <body class="ie9"> }
if (Request.Browser.MajorVersion > 9) { <body> }
} else {
<body>
}
}

这是浏览器尝试呈现它时返回的错误:

Parser Error Message: The code block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.



什么鬼?我能够在标准的 ASP.NET 模板语法中做到这一点!这是它的样子:
<% // Adaptation of paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/
if ( (Request.Browser.Browser == "IE") && (Request.Browser.MajorVersion == 7) ) { %><body class="ie7"><% } %>
<% if ( (Request.Browser.Browser == "IE") && (Request.Browser.MajorVersion == 8) ) { %><body class="ie8"><% } %>
<% if ( (Request.Browser.Browser == "IE") && (Request.Browser.MajorVersion == 9) ) { %><body class="ie9"><% } %>
<% if ( (Request.Browser.Browser == "IE") && (Request.Browser.MajorVersion > 9) ) { %><body><% } %>
<% if (Request.Browser.Browser != "IE") { %><body><% } %>

最佳答案

更好的选择可能是声明 ieClass View 顶部的变量,然后在 body 标签中引用它。

@{
string ieClass = "";
if (Request.Browser.Browser == "IE") {
if (Request.Browser.MajorVersion == 7) { ieClass="ie7"; }
else if (Request.Browser.MajorVersion == 8) { ieClass="ie8"; }
else if (Request.Browser.MajorVersion == 9) { ieClass="ie9"; }
}
}

...
<body class="@ieClass">

关于asp.net-mvc-3 - Razor Engine - 如何根据不同条件呈现不同的 body 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7812607/

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