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
31111 questions
760
votes
11 answers

How do I import a namespace in Razor View Page?

How to import a namespace in Razor View Page?
Amitabh
  • 51,891
  • 40
  • 102
  • 154
601
votes
15 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
  • 16,085
  • 3
  • 33
  • 44
452
votes
12 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
  • 26,260
  • 55
  • 172
  • 278
439
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,423
  • 2
  • 13
  • 4
430
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
  • 30,925
  • 24
  • 79
  • 101
388
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
  • 11,413
  • 12
  • 50
  • 79
382
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…
Chev
  • 54,842
  • 60
  • 203
  • 309
381
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,029
  • 27
  • 94
  • 151
343
votes
25 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
  • 54,046
  • 58
  • 232
  • 321
340
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
  • 16,875
  • 15
  • 76
  • 114
323
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
  • 11,848
  • 24
  • 69
  • 100
322
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,107
  • 14
  • 56
  • 76
304
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
  • 69,608
  • 55
  • 212
  • 331
258
votes
9 answers

Serving favicon.ico in ASP.NET MVC

What is the final/best recommendation for how to serve favicon.ico in ASP.NET MVC? I am currently doing the following: Adding an entry to the very beginning of my RegisterRoutes method: routes.IgnoreRoute("favicon.ico"); Placing favicon.ico in the…
Simon_Weaver
  • 120,240
  • 73
  • 577
  • 618
256
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
  • 49,467
  • 20
  • 108
  • 126
1
2 3
99 100