Questions tagged [razor]

Razor is a template language used by ASP.NET Web Pages, ASP.NET MVC (since version 3), and ASP.NET Core. It adds a layer of abstraction above HTML generation. It supports seamless transitions between HTML markup and C# or VB code. Transitions between markup and code are indicated by the "@" sign.

Razor is a template language used by ASP.NET Web Pages, ASP.NET MVC (since version 3), and ASP.NET Core. It supports seamless transitions between HTML markup and C# or VB code. Razor files are of extension type .cshtml (for C#) and .vbhtml (for VB). Instead of a "Code Behind File" with your C# or VB code, you can inject your code in the same file with your HTML markup. Transitions between markup and code are indicated by the "@" sign.

For example, to render a simple HTML list, this C# syntax is used:

<ul>
@for (int i = 0; i < 10; i++) {
    <li>Item @i</li>
}
</ul>

To render a simple HTML list in VB, this syntax is used:

<ul>
@For i As Integer = 0 To 9
    @<li>Item @i</li>    
Next
</ul>

Razor has support for helper templates:

@helper Bold(string text) {
   return "<bold>"+text+"<bold>";
}

<p>
  This text is @Bold("bold")
<p> 

By default all string are html encoded, if you wish to avoid that use the Raw helper:

<p>@Html.Raw("<bold>hello</bold>")</p>

Occasionally you may want to include text in an escaped section, to do so use <text> or @::

@if(condition) {
 @: This is going to be rendered
}

@if(condition) {
 <text>
   This is a 
   Multiline text block 
 </text>
} 

Reference articles


  1. C# Razor Syntax Quick Reference
  2. Introduction to ASP.NET Web Programming Using the Razor Syntax (C#)
  3. Razor syntax reference for ASP.NET Core
33007 questions
784
votes
11 answers

How do I import a namespace in Razor View Page?

How to import a namespace in Razor View Page?
Amitabh
  • 59,111
  • 42
  • 110
  • 159
664
votes
16 answers

Escape @ character in razor view engine

I am creating a sample ASP.NET MVC 3 site using Razor as view engine. The razor syntax starts with @ character e.g. @RenderBody(). If I write @test on my cshtml page it gives me parse error CS0103: The name 'test' does not exist in the current…
ajay_whiz
  • 17,573
  • 4
  • 36
  • 44
477
votes
7 answers

How to use ternary operator in razor (specifically on HTML attributes)?

With the WebForms view engine, I'll commonly use the ternary operator for very simple conditionals, especially within HTML attributes. For example: ">My link here The above code…
Portman
  • 31,785
  • 25
  • 82
  • 101
470
votes
13 answers

Using Razor within JavaScript

Is it possible or is there a workaround to use Razor syntax within JavaScript that is in a view (cshtml)? I am trying to add markers to a Google map... For example, I tried this, but I'm getting a ton of compilation errors:
raklos
  • 28,027
  • 60
  • 183
  • 301
461
votes
7 answers

Writing/outputting HTML strings unescaped

I've got safe/sanitized HTML saved in a DB table. How can I have this HTML content written out in a Razor view? It always escapes characters like < and ampersands to &.
AGS
  • 4,643
  • 2
  • 15
  • 4
425
votes
7 answers

How to declare a local variable in Razor?

I am developing a web application in asp.net mvc 3. I am very new to it. In a view using razor, I'd like to declare some local variables and use it across the entire page. How can this be done? It seems rather trivial to be able to do the following…
vondip
  • 13,809
  • 27
  • 100
  • 156
402
votes
10 answers

How to get current page URL in MVC 3

I am using the Facebook comments plugin on a blog I am building. It has some FBXML tags that are interpreted by the facebook javascript that is referenced on the page. This all works fine, but I have to pass in the current, fully-qualified URL to…
CatDadCode
  • 58,507
  • 61
  • 212
  • 318
393
votes
8 answers

Styles.Render in MVC4

In a .NET MVC4 project how does @Styles.Render works? I mean, in @Styles.Render("~/Content/css") which file is it calling? I dont have a file or a folder called "css" inside my Content folder.
Ricardo Polo Jaramillo
  • 12,110
  • 13
  • 58
  • 83
379
votes
23 answers

Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine

I have this section defined in my _Layout.cshtml @RenderSection("Scripts", false) I can easily use it from a view: @section Scripts { @*Stuff comes here*@ } What I'm struggling with is how to get some content injected inside this section…
tugberk
  • 57,477
  • 67
  • 243
  • 335
345
votes
6 answers

HTML.ActionLink vs Url.Action in ASP.NET Razor

Is there any difference between HTML.ActionLink vs Url.Action or they are just two ways of doing the same thing? When should I prefer one over the other?
Pankaj Upadhyay
  • 12,966
  • 24
  • 73
  • 104
342
votes
6 answers

ASP.NET MVC View Engine Comparison

I've been searching on SO & Google for a breakdown of the various View Engines available for ASP.NET MVC, but haven't found much more than simple high-level descriptions of what a view engine is. I'm not necessarily looking for "best" or "fastest"…
mckamey
  • 17,359
  • 16
  • 83
  • 116
332
votes
3 answers

How do I specify different Layouts in the ASP.NET MVC 3 razor ViewStart file?

I would like to have 2 separate Layouts in my application. Let's say one is for the Public section of the website and the other is for the Member side. For simplicity, let's say all the logic for each of these sites is wrapped neatly into 2 distinct…
Justin
  • 10,667
  • 15
  • 58
  • 79
309
votes
5 answers

ASP.NET MVC 3 - Partial vs Display Template vs Editor Template

So, the title should speak for itself. To create re-usable components in ASP.NET MVC, we have 3 options (could be others i haven't mentioned): Partial View: @Html.Partial(Model.Foo, "SomePartial") Custom Editor Template: @Html.EditorFor(model =>…
RPM1984
  • 72,246
  • 58
  • 225
  • 350
285
votes
26 answers

How to get the Display Name Attribute of an Enum member via MVC Razor code?

I've got a property in my model called Promotion that its type is a flag enum called UserPromotion. Members of my enum have display attributes set as follows: [Flags] public enum UserPromotion { None = 0x0, [Display(Name = "Send Job Offers…
Pejman
  • 3,784
  • 4
  • 24
  • 33
278
votes
7 answers

Replace line break characters with
in ASP.NET MVC Razor view

I have a textarea control that accepts input. I am trying to later render that text to a view by simply using: @Model.CommentText This is properly encoding any values. However, I want to replace the line break characters with
and I can't…
bkaid
  • 51,465
  • 22
  • 112
  • 128
1
2 3
99 100