Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor Engine - How can I render different body tags based on different conditions?

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><% } %>
like image 605
Joshua Hayworth Avatar asked Oct 18 '11 19:10

Joshua Hayworth


People also ask

Is razor pages better than MVC?

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.

Can you mix razor pages and MVC?

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.

What is @model in razor?

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.

How does a Razor view engine work?

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.


1 Answers

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">
like image 95
jrummell Avatar answered Sep 27 '22 23:09

jrummell