Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of Razor expressions with or without string quotes

Tags:

c#

asp.net

razor

Regarding Razor expression usage (ASP.Net) cases, there are two cases commonly met:

<div title="@MyClass.Test"></div>

and

<div [email protected]></div>

As they both seem to work the same on Razor 2.0, I would like to know if there are any differences, especially in corner cases (null/malfored values etc).

Eventually I would also like to know which is best as a practice to follow. Thank you in advance.

like image 591
Nick Louloudakis Avatar asked Jul 07 '17 07:07

Nick Louloudakis


People also ask

What are Razor expressions used for?

Razor Expression Encoding Razor provides expression encoding to avoid malicious code and security risks. In case, if user enters a malicious script as input, razor engine encode the script and render as HTML output.

What is Razor in MVC with example?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine.

Which of the following represent Razor syntax?

The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a . cshtml file extension.

What is a Razor Web page?

What is Razor? Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages. Server-based code can create dynamic web content on the fly, while a web page is written to the browser.


1 Answers

The rules for HTML attributes can be found here. The quotes are usually optional, but if there is a space in the value of MyClass.Test then you'll need them.

I would say best practice is to include them as it doesn't hurt and will save you problems if there are spaces in the value.

like image 144
Andy Nichols Avatar answered Oct 26 '22 11:10

Andy Nichols