Here is the code I currently have:
@{
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>
}
}
Here is the error that it returns when the browser attempts to render it:
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.
What the heck? I was able to do this in the standard ASP.NET template syntax! Here's what that looked like:
<% // 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><% } %>
From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.
You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder.
New @model directive Let's now look at a new feature we added with the ASP.NET MVC 3 Beta – the @model directive. The @model directive provides a cleaner and more concise way to reference strongly-typed models from view files.
The namespace for Razor Engine is System. Razor uses the "@" character instead of "<% %>" as used by the ASPX View Engine. The Razor file extension is "cshtml" for the C# language. By default, Razor View Engine encodes html tags or scripts before it's being rendered to view that avoids Cross-Site Scripting attacks.
A better option might be to declare an ieClass
variable at the top of your view, and then reference it in your body tag.
@{
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">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With