Lỗi only one model statement is allowed in a file năm 2024

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Razor syntax reference for ASP.NET Core

  • Article
  • 11/14/2023

In this article

By Rick Anderson, Taylor Mullen, and Dan Vicarel

Razor is a markup syntax for embedding .NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a

Last week: @DateTime.Now - TimeSpan.FromDays(7)

2 file extension. Razor is also found in Razor component files (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3). Razor syntax is similar to the templating engines of various JavaScript single-page application (SPA) frameworks, such as Angular, React, VueJs, and Svelte. For more information see, The features described in this article are obsolete as of ASP.NET Core 3.0.

Introduction to ASP.NET Web Programming Using the Razor Syntax provides many samples of programming with Razor syntax. Although the topic was written for ASP.NET rather than ASP.NET Core, most of the samples apply to ASP.NET Core.

Rendering HTML

The default Razor language is HTML. Rendering HTML from Razor markup is no different than rendering HTML from an HTML file. HTML markup in

Last week: @DateTime.Now - TimeSpan.FromDays(7)

2 Razor files is rendered by the server unchanged.

Razor syntax

Razor supports C# and uses the

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 symbol to transition from HTML to C#. Razor evaluates C# expressions and renders them in the HTML output.

When an

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 symbol is followed by a , it transitions into Razor-specific markup. Otherwise, it transitions into plain HTML.

To escape an

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 symbol in Razor markup, use a second

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 symbol:

@@Username

The code is rendered in HTML with a single

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 symbol:

@Username

HTML attributes and content containing email addresses don't treat the

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 symbol as a transition character. The email addresses in the following example are untouched by Razor parsing:

[email protected]

Scalable Vector Graphics (SVG)

SVG foreignObject elements are supported:

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

Implicit Razor expressions

Implicit Razor expressions start with

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 followed by C# code:

@DateTime.Now

@DateTime.IsLeapYear(2016)

With the exception of the C#

Last week: 7/7/2016 4:39:52 PM - TimeSpan.FromDays(7)

2 keyword, implicit expressions must not contain spaces. If the C# statement has a clear ending, spaces can be intermingled:

@await DoSomething("hello", "world")

Implicit expressions cannot contain C# generics, as the characters inside the brackets (

Last week: 7/7/2016 4:39:52 PM - TimeSpan.FromDays(7)

  1. are interpreted as an HTML tag. The following code is not valid:

@GenericMethod()

The preceding code generates a compiler error similar to one of the following:

  • The "int" element wasn't closed. All elements must be either self-closing or have a matching end tag.
  • Cannot convert method group 'GenericMethod' to non-delegate type 'object'. Did you intend to invoke the method?`

Generic method calls must be wrapped in an or a .

Explicit Razor expressions

Explicit Razor expressions consist of an

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 symbol with balanced parenthesis. To render last week's time, the following Razor markup is used:

Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))

Any content within the

Last week: 7/7/2016 4:39:52 PM - TimeSpan.FromDays(7)

5 parenthesis is evaluated and rendered to the output.

Implicit expressions, described in the previous section, generally can't contain spaces. In the following code, one week isn't subtracted from the current time:

Last week: @DateTime.Now - TimeSpan.FromDays(7)

The code renders the following HTML:

Last week: 7/7/2016 4:39:52 PM - TimeSpan.FromDays(7)

Explicit expressions can be used to concatenate text with an expression result:

@Username

0

Without the explicit expression,

Last week: 7/7/2016 4:39:52 PM - TimeSpan.FromDays(7)

6 is treated as an email address, and

Last week: 7/7/2016 4:39:52 PM - TimeSpan.FromDays(7)

6 is rendered. When written as an explicit expression,

Last week: 7/7/2016 4:39:52 PM - TimeSpan.FromDays(7)

8 is rendered.

Explicit expressions can be used to render output from generic methods in

Last week: @DateTime.Now - TimeSpan.FromDays(7)

2 files. The following markup shows how to correct the error shown earlier caused by the brackets of a C# generic. The code is written as an explicit expression:

@Username

1

Expression encoding

C# expressions that evaluate to a string are HTML encoded. C# expressions that evaluate to

@Username

00 are rendered directly through

@Username

01. C# expressions that don't evaluate to

@Username

00 are converted to a string by

@Username

03 and encoded before they're rendered.

@Username

2

The preceding code renders the following HTML:

@Username

3

The HTML is shown in the browser as plain text:

Hello World

@Username

04 output isn't encoded but rendered as HTML markup.

Warning

Using

@Username

04 on unsanitized user input is a security risk. User input might contain malicious JavaScript or other exploits. Sanitizing user input is difficult. Avoid using

@Username

04 with user input.

@Username

4

The code renders the following HTML:

@Username

5

Razor code blocks

Razor code blocks start with

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 and are enclosed by

@Username

08. Unlike expressions, C# code inside code blocks isn't rendered. Code blocks and expressions in a view share the same scope and are defined in order:

@Username

6

The code renders the following HTML:

@Username

7

In code blocks, declare local functions with markup to serve as templating methods:

@Username

8

The code renders the following HTML:

@Username

9

Implicit transitions

The default language in a code block is C#, but the Razor Page can transition back to HTML:

[email protected]

0

Explicit delimited transition

To define a subsection of a code block that should render HTML, surround the characters for rendering with the Razor

@Username

09 tag:

[email protected]

1

Use this approach to render HTML that isn't surrounded by an HTML tag. Without an HTML or Razor tag, a Razor runtime error occurs.

The

@Username

09 tag is useful to control whitespace when rendering content:

  • Only the content between the

    @Username

    09 tag is rendered.
  • No whitespace before or after the

    @Username

    09 tag appears in the HTML output.

Explicit line transition

To render the rest of an entire line as HTML inside a code block, use

@Username

13 syntax:

[email protected]

2

Without the

@Username

13 in the code, a Razor runtime error is generated.

Extra

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 characters in a Razor file can cause compiler errors at statements later in the block. These extra

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 compiler errors:

  • Can be difficult to understand because the actual error occurs before the reported error.
  • Is common after combining multiple implicit and explicit expressions into a single code block.

Conditional attribute rendering

Razor automatically omits attributes that aren't needed. If the value passed in is

@Username

17 or

@Username

18, the attribute isn't rendered.

For example, consider the following razor:

[email protected]

3

The preceding Razor markup generates the following HTML:

[email protected]

4

Control structures

Control structures are an extension of code blocks. All aspects of code blocks (transitioning to markup, inline C#) also apply to the following structures:

Conditionals

@Username

19

@Username

20 controls when code runs:

[email protected]

5

@Username

21 and

@Username

22 don't require the

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 symbol:

[email protected]

6

The following markup shows how to use a switch statement:

[email protected]

7

Looping

@Username

24

Templated HTML can be rendered with looping control statements. To render a list of people:

[email protected]

8

The following looping statements are supported:

@Username

25

[email protected]

9

@Username

26

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

0

@Username

27

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

1

@Username

28

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

2

Compound

@Username

29

In C#, a

@Username

30 statement is used to ensure an object is disposed. In Razor, the same mechanism is used to create HTML Helpers that contain additional content. In the following code, HTML Helpers render a

@Username

31 tag with the

@Username

29 statement:

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

3

@Username

33

Exception handling is similar to C#:

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

4

@Username

34

Razor has the capability to protect critical sections with lock statements:

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

5

Comments

Razor supports C# and HTML comments:

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

6

The code renders the following HTML:

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

7

Razor comments are removed by the server before the webpage is rendered. Razor uses

@Username

35 to delimit comments. The following code is commented out, so the server doesn't render any markup:

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

8

Directives

Razor directives are represented by implicit expressions with reserved keywords following the

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 symbol. A directive typically changes the way a view is compiled or functions.

Understanding how Razor generates code for a view makes it easier to understand how directives work.

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

9

The code generates a class similar to the following:

@DateTime.Now

@DateTime.IsLeapYear(2016)

0

Later in this article, the section explains how to view this generated class.

@Username

37

The

@Username

37 directive adds the given attribute to the class of the generated page or view. The following example adds the

@Username

39 attribute:

@DateTime.Now

@DateTime.IsLeapYear(2016)

1

The

@Username

37 directive can also be used to supply a constant-based route template in a Razor component. In the following example, the

@Username

41 directive in a component is replaced with the

@Username

37 directive and the constant-based route template in

@Username

43, which is set elsewhere in the app to "

@Username

44":

@DateTime.Now

@DateTime.IsLeapYear(2016)

2

@Username

45

This scenario only applies to Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3).

The

@Username

45 block enables a Razor component to add C# members (fields, properties, and methods) to a component:

@DateTime.Now

@DateTime.IsLeapYear(2016)

3

For Razor components,

@Username

45 is an alias of and recommended over

@Username

49. More than one

@Username

45 block is permissible.

@Username

49

The

@Username

49 directive enables adding C# members (fields, properties, and methods) to the generated class:

@DateTime.Now

@DateTime.IsLeapYear(2016)

4

In Razor components, use

@Username

45 over

@Username

49 to add C# members.

For example:

@DateTime.Now

@DateTime.IsLeapYear(2016)

5

The code generates the following HTML markup:

@DateTime.Now

@DateTime.IsLeapYear(2016)

6

The following code is the generated Razor C# class:

@DateTime.Now

@DateTime.IsLeapYear(2016)

7

@Username

49 methods serve as templating methods when they have markup:

@DateTime.Now

@DateTime.IsLeapYear(2016)

8

The code renders the following HTML:

@Username

9

@Username

57

The

@Username

57 directive implements an interface for the generated class.

The following example implements System.IDisposable so that the Dispose method can be called:

@await DoSomething("hello", "world")

0

@Username

59

The

@Username

59 directive provides full control of the class the view inherits:

@await DoSomething("hello", "world")

1

The following code is a custom Razor page type:

@await DoSomething("hello", "world")

2

The

@Username

61 is displayed in a view:

@await DoSomething("hello", "world")

3

The code renders the following HTML:

@await DoSomething("hello", "world")

4

@Username

62 and

@Username

59 can be used in the same view.

@Username

59 can be in a

@Username

65 file that the view imports:

@await DoSomething("hello", "world")

5

The following code is an example of a strongly-typed view:

@await DoSomething("hello", "world")

6

If "[email protected]" is passed in the model, the view generates the following HTML markup:

@await DoSomething("hello", "world")

7

@Username

66

The

@Username

66 directive enables the Razor Page to inject a service from the service container into a view. For more information, see Dependency injection into views.

@Username

68

This scenario only applies to Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3).

The

@Username

68 directive specifies a layout for routable Razor components that have an directive. Layout components are used to avoid code duplication and inconsistency. For more information, see ASP.NET Core Blazor layouts.

@Username

62

This scenario only applies to MVC views and Razor Pages (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

2).

The

@Username

62 directive specifies the type of the model passed to a view or page:

@await DoSomething("hello", "world")

8

In an ASP.NET Core MVC or Razor Pages app created with individual user accounts,

@Username

75 contains the following model declaration:

@await DoSomething("hello", "world")

9

The class generated inherits from

@Username

76:

@GenericMethod()

0

Razor exposes a

@Username

77 property for accessing the model passed to the view:

@GenericMethod()

1

The

@Username

62 directive specifies the type of the

@Username

77 property. The directive specifies the

@Username

80 in

@Username

81 that the generated class that the view derives from. If the

@Username

62 directive isn't specified, the

@Username

77 property is of type

@Username

84. For more information, see .

@Username

85

The

@Username

85 directive:

  • Sets the namespace of the class of the generated Razor page, MVC view, or Razor component.
  • Sets the root derived namespaces of a pages, views, or components classes from the closest imports file in the directory tree,

    @Username

    65 (views or pages) or

    @Username

    88 (Razor components).

@GenericMethod()

2

For the Razor Pages example shown in the following table:

  • Each page imports

    @Username

    89.
  • @Username

    89 contains

    @Username

    91.
  • Each page has

    @Username

    92 as the root of it's namespace. Page Namespace

@Username

93

@Username

92

@Username

95

@Username

96

@Username

97

@Username

98

The preceding relationships apply to import files used with MVC views and Razor components.

When multiple import files have a

@Username

85 directive, the file closest to the page, view, or component in the directory tree is used to set the root namespace.

If the

[email protected]

00 folder in the preceding example has an imports file with

[email protected]

01 (or the

@Username

97 file contains

[email protected]

01), the result is shown in the following table.

Page Namespace

@Username

93

@Username

92

@Username

95

@Username

96

@Username

97

[email protected]

09

@Username

41

The

@Username

41 directive has different effects depending on the type of the file where it appears. The directive:

  • In a

    Last week: @DateTime.Now - TimeSpan.FromDays(7)

    2 file indicates that the file is a Razor Page. For more information, see and Introduction to Razor Pages in ASP.NET Core.
  • Specifies that a Razor component should handle requests directly. For more information, see ASP.NET Core Blazor routing and navigation.

[email protected]

13

This scenario only applies to Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3).

When set to

@Username

18 (default), whitespace in the rendered markup from Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

  1. is removed if:
  • Leading or trailing within an element.
  • Leading or trailing within a

    [email protected]

    17 parameter. For example, child content passed to another component.
  • It precedes or follows a C# code block, such as

    @Username

    20 or

    @Username

    26.

[email protected]

20

This scenario only applies to Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3).

Sets the render mode of a Razor component:

  • [email protected]

    22: Applies interactive server rendering using Blazor Server.
  • [email protected]

    23: Applies interactive WebAssembly rendering using Blazor WebAssembly.
  • [email protected]

    24: Initially applies interactive WebAssembly rendering using Blazor Server, and then applies interactive WebAssembly rendering using WebAssembly on subsequent visits after the Blazor bundle is downloaded.

For a component instance:

@GenericMethod()

3

In the component definition:

@GenericMethod()

4

Note

Blazor templates include a static

@Username

30 directive for RenderMode in the app's

[email protected]

26 file (

[email protected]

  1. for shorter

[email protected]

20 syntax:

@GenericMethod()

5

Without the preceding directive, components must specify the static RenderMode class in

[email protected]

20 syntax explicitly:

@GenericMethod()

6

For more information, including guidance on disabling prerendering with the directive/directive attribute, see ASP.NET Core Blazor render modes.

[email protected]

30

This scenario only applies to MVC views and Razor Pages (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

2).

The

[email protected]

30 directive is used in conjunction with MVC and Razor Pages layouts to enable views or pages to render content in different parts of the HTML page. For more information, see Layout in ASP.NET Core.

[email protected]

33

This scenario only applies to Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3).

The

[email protected]

33 directive declares a generic type parameter for the generated component class:

@GenericMethod()

7

Generic types with

[email protected]

36 type constraints are supported:

@GenericMethod()

8

For more information, see the following articles:

  • ASP.NET Core Razor component generic type support
  • ASP.NET Core Blazor templated components

@Username

29

The

@Username

29 directive adds the C#

@Username

30 directive to the generated view:

@GenericMethod()

9

In Razor components,

@Username

29 also controls which components are in scope.

Directive attributes

Razor directive attributes are represented by implicit expressions with reserved keywords following the

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 symbol. A directive attribute typically changes the way an element is compiled or functions.

[email protected]

42

This scenario only applies to Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3).

[email protected]

42 allows a component to render non-declared attributes. For more information, see ASP.NET Core Blazor attribute splatting and arbitrary parameters.

[email protected]

45

This scenario only applies to Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3).

Data binding in components is accomplished with the

[email protected]

45 attribute. For more information, see ASP.NET Core Blazor data binding.

[email protected]

48

This scenario only applies to Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3).

Use the

[email protected]

48 attribute with the attribute to provide a System.Globalization.CultureInfo for parsing and formatting a value. For more information, see .

[email protected]

52

This scenario only applies to Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3).

[email protected]

52 assigns a form name to a Razor component's plain HTML form or a form based on EditForm (). The value of

[email protected]

52 should be unique, which prevents form collisions in the following situations:

  • A form is placed in a component with multiple forms.
  • A form is sourced from an external class library, commonly a NuGet package, for a component with multiple forms, and the app author doesn't control the source code of the library to set a different external form name than a name used by another form in the component.

For more information and examples, see ASP.NET Core Blazor forms overview.

[email protected]

57

This scenario only applies to Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3).

Razor provides event handling features for components. For more information, see ASP.NET Core Blazor event handling.

[email protected]

59

This scenario only applies to Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3).

Prevents the default action for the event.

[email protected]

61

This scenario only applies to Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3).

Stops event propagation for the event.

[email protected]

63

This scenario only applies to Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3).

The

[email protected]

63 directive attribute causes the components diffing algorithm to guarantee preservation of elements or components based on the key's value. For more information, see Retain element, component, and model relationships in ASP.NET Core Blazor.

[email protected]

66

This scenario only applies to Razor components (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

3).

Component references (

[email protected]

  1. provide a way to reference a component instance so that you can issue commands to that instance. For more information, see .

Templated Razor delegates

This scenario only applies to MVC views and Razor Pages (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

2).

Razor templates allow you to define a UI snippet with the following format:

Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))

0

The following example illustrates how to specify a templated Razor delegate as a Func. The dynamic type is specified for the parameter of the method that the delegate encapsulates. An object type is specified as the return value of the delegate. The template is used with a List of

[email protected]

70 that has a

[email protected]

71 property.

Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))

1

Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))

2

The template is rendered with

[email protected]

72 supplied by a

[email protected]

73 statement:

Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))

3

Rendered output:

Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))

4

You can also supply an inline Razor template as an argument to a method. In the following example, the

[email protected]

74 method receives a Razor template. The method uses the template to produce HTML content with repeats of items supplied from a list:

Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))

5

Using the list of pets from the prior example, the

[email protected]

74 method is called with:

  • List of

    [email protected]

    70.
  • Number of times to repeat each pet.
  • Inline template to use for the list items of an unordered list.

Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))

6

Rendered output:

Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))

7

Tag Helpers

This scenario only applies to MVC views and Razor Pages (

Last week: @DateTime.Now - TimeSpan.FromDays(7)

2).

There are three directives that pertain to Tag Helpers.

Directive Function Makes Tag Helpers available to a view. Removes Tag Helpers previously added from a view. Specifies a tag prefix to enable Tag Helper support and to make Tag Helper usage explicit.

Razor reserved keywords

Razor keywords

Razor keywords are escaped with

[email protected]

88 (for example,

[email protected]

89).

C# Razor keywords

C# Razor keywords must be double-escaped with

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

04 (for example,

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

05). The first

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 escapes the Razor parser. The second

Last week: @DateTime.Now - TimeSpan.FromDays(7)

5 escapes the C# parser.

Reserved keywords not used by Razor

  • @{

    string message = "foreignObject example with Scalable Vector Graphics (SVG)";  
    
    }
      
      
        

    @message

    08

Inspect the Razor C# class generated for a view

The Razor SDK handles compilation of Razor files. By default, the generated code files aren't emitted. To enable emitting the code files, set the

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

09 directive in the project file (

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

  1. to

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

11:

Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))

8

When building a 6.0 project (

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

  1. in the

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

13 build configuration, the Razor SDK generates an

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

14 directory in the project root. Its subdirectory contains the emitted Razor page code files.

The Razor SDK handles compilation of Razor files. When building a project, the Razor SDK generates an

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

15 directory in the project root. The directory structure within the

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

16 directory mirrors the project's directory structure.

Consider the following directory structure in an ASP.NET Core Razor Pages 2.1 project:

Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))

9

Building the project in

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

13 configuration yields the following

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

18 directory:

Last week: @DateTime.Now - TimeSpan.FromDays(7)

0

To view the generated class for

@Username

93, open

@{
    string message = "foreignObject example with Scalable Vector Graphics (SVG)";
}

    
    
        

@message

20.

View lookups and case sensitivity

The Razor view engine performs case-sensitive lookups for views. However, the actual lookup is determined by the underlying file system:

  • File based source:
    • On operating systems with case insensitive file systems (for example, Windows), physical file provider lookups are case insensitive. For example,

      @{

         string message = "foreignObject example with Scalable Vector Graphics (SVG)";  
      
      }
           
           
             

      @message

      21 results in matches for

      @{

         string message = "foreignObject example with Scalable Vector Graphics (SVG)";  
      
      }
           
           
             

      @message

      22,

      @{

         string message = "foreignObject example with Scalable Vector Graphics (SVG)";  
      
      }
           
           
             

      @message

      23, and any other casing variant.
    • On case-sensitive file systems (for example, Linux, OSX, and with

      @{

         string message = "foreignObject example with Scalable Vector Graphics (SVG)";  
      
      }
           
           
             

      @message

      24), lookups are case-sensitive. For example,

      @{

         string message = "foreignObject example with Scalable Vector Graphics (SVG)";  
      
      }
           
           
             

      @message

      21 specifically matches

      @{

         string message = "foreignObject example with Scalable Vector Graphics (SVG)";  
      
      }
           
           
             

      @message

      22.
  • Precompiled views: With ASP.NET Core 2.0 and later, looking up precompiled views is case insensitive on all operating systems. The behavior is identical to physical file provider's behavior on Windows. If two precompiled views differ only in case, the result of lookup is non-deterministic.

Developers are encouraged to match the casing of file and directory names to the casing of:

  • Area, controller, and action names.
  • Razor Pages.

Matching case ensures the deployments find their views regardless of the underlying file system.

Imports used by Razor

The following imports are generated by the ASP.NET Core web templates to support Razor Files:

Last week: @DateTime.Now - TimeSpan.FromDays(7)

1

Additional resources

Introduction to ASP.NET Web Programming Using the Razor Syntax provides many samples of programming with Razor syntax.

Collaborate with us on GitHub

The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.