Razor​Page Razor​Page Class

Definition

Represents properties and methods that are needed in order to render a view that uses Razor syntax.
public abstract class RazorPage : IRazorPagePublic MustInherit Class RazorPage
    Implements IRazorPage
Inheritance
System.ObjectSystem.Object
RazorPageRazorPage
Derived
Implements

Inherited Members

System.Object

System.Object.ToString()System.Object.ToString()
System.Object.Equals(System.Object)System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()System.Object.GetHashCode()
System.Object.GetType()System.Object.GetType()
System.Object.MemberwiseClone()System.Object.MemberwiseClone()

Constructors

RazorPage() RazorPage()

public RazorPage()Public Sub New

Properties

BodyContent BodyContent

Gets or sets the body content.
public IHtmlContent BodyContent { get; set; }Public Property BodyContent As IHtmlContent

Context Context

An HttpContext representing the current request execution.
public HttpContext Context { get; }Public ReadOnly Property Context As HttpContext

DiagnosticSource DiagnosticSource

Gets or sets a System.Diagnostics.DiagnosticSource.#ctor instance used to instrument the page execution.
public DiagnosticSource DiagnosticSource { get; set; }Public Property DiagnosticSource As DiagnosticSource
Value
System.Diagnostics.DiagnosticSource System.Diagnostics.DiagnosticSource

HtmlEncoder HtmlEncoder

Gets the System.Text.Encodings.Web.HtmlEncoder to use when this RazorPage handles non-IHtmlContent C# expressions.
public HtmlEncoder HtmlEncoder { get; set; }Public Property HtmlEncoder As HtmlEncoder
Value
System.Text.Encodings.Web.HtmlEncoder System.Text.Encodings.Web.HtmlEncoder

IsLayoutBeingRendered IsLayoutBeingRendered

Gets or sets a flag that determines if the layout of this page is being rendered.
public bool IsLayoutBeingRendered { get; set; }Public Property IsLayoutBeingRendered As Boolean
Value
System.Boolean System.Boolean

Remarks

Sections defined in a page are deferred and executed as part of the layout page. When this flag is set, all write operations performed by the page are part of a section being rendered.

Layout Layout

Gets or sets the path of a layout page.
public string Layout { get; set; }Public Property Layout As String
Value
System.String System.String

Output Output

Gets the System.IO.TextWriter that the page is writing output to.
public virtual TextWriter Output { get; }Public Overridable ReadOnly Property Output As TextWriter
Value
System.IO.TextWriter System.IO.TextWriter

Path Path

Gets the application base relative path to the page.
public string Path { get; set; }Public Property Path As String
Value
System.String System.String

PreviousSectionWriters PreviousSectionWriters

Gets or sets the sections that can be rendered by this page.
public IDictionary<string, RenderAsyncDelegate> PreviousSectionWriters { get; set; }Public Property PreviousSectionWriters As IDictionary(Of String, RenderAsyncDelegate)
Value
System.Collections.Generic.IDictionary<System.String, RenderAsyncDelegate> System.Collections.Generic.IDictionary(Of System.String, RenderAsyncDelegate)

SectionWriters SectionWriters

Gets the sections that are defined by this page.
public IDictionary<string, RenderAsyncDelegate> SectionWriters { get; }Public ReadOnly Property SectionWriters As IDictionary(Of String, RenderAsyncDelegate)
Value
System.Collections.Generic.IDictionary<System.String, RenderAsyncDelegate> System.Collections.Generic.IDictionary(Of System.String, RenderAsyncDelegate)

TempData TempData

Gets the ITempDataDictionary from the ViewContext.
public ITempDataDictionary TempData { get; }Public ReadOnly Property TempData As ITempDataDictionary

Remarks

Returns null if ViewContext is null.

User User

Gets the System.Security.Claims.ClaimsPrincipal of the current logged in user.
public virtual ClaimsPrincipal User { get; }Public Overridable ReadOnly Property User As ClaimsPrincipal
Value
System.Security.Claims.ClaimsPrincipal System.Security.Claims.ClaimsPrincipal

ViewBag ViewBag

Gets the dynamic view data dictionary.
public dynamic ViewBag { get; }Public ReadOnly Property ViewBag As Object
Value
System.Object System.Object

ViewContext ViewContext

Gets or sets the view context of the renderign view.
public ViewContext ViewContext { get; set; }Public Property ViewContext As ViewContext

Methods

AddHtmlAttributeValue(String, Int32, Object, Int32, Int32, Boolean) AddHtmlAttributeValue(String, Int32, Object, Int32, Int32, Boolean)

public void AddHtmlAttributeValue(string prefix, int prefixOffset, object value, int valueOffset, int valueLength, bool isLiteral)Public Sub AddHtmlAttributeValue(prefix As String, prefixOffset As Integer, value As Object, valueOffset As Integer, valueLength As Integer, isLiteral As Boolean)
Parameters
prefix
System.String System.String
prefixOffset
System.Int32 System.Int32
value
System.Object System.Object
valueOffset
System.Int32 System.Int32
valueLength
System.Int32 System.Int32
isLiteral
System.Boolean System.Boolean

BeginAddHtmlAttributeValues(TagHelperExecutionContext, String, Int32, HtmlAttributeValueStyle) BeginAddHtmlAttributeValues(TagHelperExecutionContext, String, Int32, HtmlAttributeValueStyle)

public void BeginAddHtmlAttributeValues(TagHelperExecutionContext executionContext, string attributeName, int attributeValuesCount, HtmlAttributeValueStyle attributeValueStyle)Public Sub BeginAddHtmlAttributeValues(executionContext As TagHelperExecutionContext, attributeName As String, attributeValuesCount As Integer, attributeValueStyle As HtmlAttributeValueStyle)
Parameters
attributeName
System.String System.String
attributeValuesCount
System.Int32 System.Int32

BeginContext(Int32, Int32, Boolean) BeginContext(Int32, Int32, Boolean)

public void BeginContext(int position, int length, bool isLiteral)Public Sub BeginContext(position As Integer, length As Integer, isLiteral As Boolean)
Parameters
position
System.Int32 System.Int32
length
System.Int32 System.Int32
isLiteral
System.Boolean System.Boolean

BeginWriteAttribute(String, String, Int32, String, Int32, Int32) BeginWriteAttribute(String, String, Int32, String, Int32, Int32)

public virtual void BeginWriteAttribute(string name, string prefix, int prefixOffset, string suffix, int suffixOffset, int attributeValuesCount)Public Overridable Sub BeginWriteAttribute(name As String, prefix As String, prefixOffset As Integer, suffix As String, suffixOffset As Integer, attributeValuesCount As Integer)
Parameters
name
System.String System.String
prefix
System.String System.String
prefixOffset
System.Int32 System.Int32
suffix
System.String System.String
suffixOffset
System.Int32 System.Int32
attributeValuesCount
System.Int32 System.Int32

BeginWriteAttributeTo(TextWriter, String, String, Int32, String, Int32, Int32) BeginWriteAttributeTo(TextWriter, String, String, Int32, String, Int32, Int32)

public virtual void BeginWriteAttributeTo(TextWriter writer, string name, string prefix, int prefixOffset, string suffix, int suffixOffset, int attributeValuesCount)Public Overridable Sub BeginWriteAttributeTo(writer As TextWriter, name As String, prefix As String, prefixOffset As Integer, suffix As String, suffixOffset As Integer, attributeValuesCount As Integer)
Parameters
writer
System.IO.TextWriter System.IO.TextWriter
name
System.String System.String
prefix
System.String System.String
prefixOffset
System.Int32 System.Int32
suffix
System.String System.String
suffixOffset
System.Int32 System.Int32
attributeValuesCount
System.Int32 System.Int32

BeginWriteTagHelperAttribute() BeginWriteTagHelperAttribute()

Starts a new scope for writing ITagHelper attribute values.
public void BeginWriteTagHelperAttribute()Public Sub BeginWriteTagHelperAttribute

Remarks

All writes to the Output or Writer after calling this method will be buffered until EndWriteTagHelperAttribute() is called. The content will be buffered using a shared System.IO.StringWriter within this RazorPage Nesting of BeginWriteTagHelperAttribute() and EndWriteTagHelperAttribute() method calls is not supported.

CreateTagHelper<TTagHelper>() CreateTagHelper(Of TTagHelper)()

Creates and activates a ITagHelper.
public TTagHelper CreateTagHelper<TTagHelper>()where TTagHelper : ITagHelperPublic Function CreateTagHelper(Of TTagHelper As ITagHelper) As TTagHelper
Type Parameters
TTagHelper
A ITagHelper type.
Returns
TTagHelper TTagHelper
The activated ITagHelper.

Remarks

TTagHelper must have a parameterless constructor.

DefineSection(String, RenderAsyncDelegate) DefineSection(String, RenderAsyncDelegate)

Creates a named content section in the page that can be invoked in a Layout page using RenderSection(String) or RenderSectionAsync(String, Boolean).
public void DefineSection(string name, RenderAsyncDelegate section)Public Sub DefineSection(name As String, section As RenderAsyncDelegate)
Parameters
name
System.String System.String
The name of the section to create.
section
RenderAsyncDelegate RenderAsyncDelegate
The RenderAsyncDelegate to execute when rendering the section.

EndAddHtmlAttributeValues(TagHelperExecutionContext) EndAddHtmlAttributeValues(TagHelperExecutionContext)

public void EndAddHtmlAttributeValues(TagHelperExecutionContext executionContext)Public Sub EndAddHtmlAttributeValues(executionContext As TagHelperExecutionContext)
Parameters

EndContext() EndContext()

public void EndContext()Public Sub

EndTagHelperWritingScope() EndTagHelperWritingScope()

Ends the current writing scope that was started by calling StartTagHelperWritingScope(HtmlEncoder).
public TagHelperContent EndTagHelperWritingScope()Public Function EndTagHelperWritingScope As TagHelperContent
Returns

EndWriteAttribute() EndWriteAttribute()

public virtual void EndWriteAttribute()Public Overridable Sub

EndWriteAttributeTo(TextWriter) EndWriteAttributeTo(TextWriter)

public virtual void EndWriteAttributeTo(TextWriter writer)Public Overridable Sub EndWriteAttributeTo(writer As TextWriter)
Parameters
writer
System.IO.TextWriter System.IO.TextWriter

EndWriteTagHelperAttribute() EndWriteTagHelperAttribute()

Ends the current writing scope that was started by calling BeginWriteTagHelperAttribute().
public string EndWriteTagHelperAttribute()Public Function EndWriteTagHelperAttribute As String
Returns
System.String System.String
The content buffered by the shared System.IO.StringWriter of this RazorPage.

Remarks

This method assumes that there will be no nesting of BeginWriteTagHelperAttribute() and EndWriteTagHelperAttribute() method calls.

EnsureRenderedBodyOrSections() EnsureRenderedBodyOrSections()

Verifies that all sections defined in PreviousSectionWriters were rendered, or the body was rendered if no sections were defined.
public void EnsureRenderedBodyOrSections()Public Sub EnsureRenderedBodyOrSections
Exceptions
System.InvalidOperationException System.InvalidOperationException
if one or more sections were not rendered or if no sections were defined and the body was not rendered.

ExecuteAsync() ExecuteAsync()

Renders the page and writes the output to the Writer.
public abstract Task ExecuteAsync()Public MustOverride Function ExecuteAsync As Task
Returns
System.Threading.Tasks.Task System.Threading.Tasks.Task
A task representing the result of executing the page.

FlushAsync() FlushAsync()

Invokes System.IO.TextWriter.FlushAsync() on Output and Stream.FlushAsync on the response stream, writing out any buffered content to the Body.
public Task<HtmlString> FlushAsync()Public Function FlushAsync As Task(Of HtmlString)
Returns
System.Threading.Tasks.Task<HtmlString> System.Threading.Tasks.Task(Of HtmlString)
A System.Threading.Tasks.Task<TResult> that represents the asynchronous flush operation and on completion returns Empty.

Remarks

The value returned is a token value that allows FlushAsync to work directly in an HTML section. However the value does not represent the rendered content. This method also writes out headers, so any modifications to headers must be done before FlushAsync() is called. For example, call SetAntiforgeryCookieAndHeader() to send antiforgery cookie token and X-Frame-Options header to client before this method flushes headers out.

Href(String) Href(String)

public virtual string Href(string contentPath)Public Overridable Function Href(contentPath As String) As String
Parameters
contentPath
System.String System.String
Returns
System.String System.String

IgnoreBody() IgnoreBody()

In a Razor layout page, ignores rendering the portion of a content page that is not within a named section.
public void IgnoreBody()Public Sub IgnoreBody

IgnoreSection(String) IgnoreSection(String)

In layout pages, ignores rendering the content of the section named sectionName.
public void IgnoreSection(string sectionName)Public Sub IgnoreSection(sectionName As String)
Parameters
sectionName
System.String System.String
The section to ignore.

InvalidTagHelperIndexerAssignment(String, String, String) InvalidTagHelperIndexerAssignment(String, String, String)

Format an error message about using an indexer when the tag helper property is null.
public static string InvalidTagHelperIndexerAssignment(string attributeName, string tagHelperTypeName, string propertyName)Public Shared Function InvalidTagHelperIndexerAssignment(attributeName As String, tagHelperTypeName As String, propertyName As String) As String
Parameters
attributeName
System.String System.String
Name of the HTML attribute associated with the indexer.
tagHelperTypeName
System.String System.String
Full name of the tag helper System.Type.
propertyName
System.String System.String
Dictionary property in the tag helper.
Returns
System.String System.String
An error message about using an indexer when the tag helper property is null.

IsSectionDefined(String) IsSectionDefined(String)

Returns a value that indicates whether the specified section is defined in the content page.
public bool IsSectionDefined(string name)Public Function IsSectionDefined(name As String) As Boolean
Parameters
name
System.String System.String
The section name to search for.
Returns
System.Boolean System.Boolean
true if the specified section is defined in the content page; otherwise, false.

RenderBody() RenderBody()

In a Razor layout page, renders the portion of a content page that is not within a named section.
protected virtual IHtmlContent RenderBody()Protected Overridable Function RenderBody As IHtmlContent
Returns
The HTML content to render.

RenderSection(String) RenderSection(String)

In layout pages, renders the content of the section named name.
public HtmlString RenderSection(string name)Public Function RenderSection(name As String) As HtmlString
Parameters
name
System.String System.String
The name of the section to render.
Returns
Returns Empty to allow the Write(Object) call to succeed.

Remarks

The method writes to the Output and the value returned is a token value that allows the Write (produced due to @RenderSection(..)) to succeed. However the value does not represent the rendered content.

RenderSection(String, Boolean) RenderSection(String, Boolean)

In layout pages, renders the content of the section named name.
public HtmlString RenderSection(string name, bool required)Public Function RenderSection(name As String, required As Boolean) As HtmlString
Parameters
name
System.String System.String
The section to render.
required
System.Boolean System.Boolean
Indicates if this section must be rendered.
Returns
Returns Empty to allow the Write(Object) call to succeed.

Remarks

The method writes to the Output and the value returned is a token value that allows the Write (produced due to @RenderSection(..)) to succeed. However the value does not represent the rendered content.

RenderSectionAsync(String) RenderSectionAsync(String)

In layout pages, asynchronously renders the content of the section named name.
public Task<HtmlString> RenderSectionAsync(string name)Public Function RenderSectionAsync(name As String) As Task(Of HtmlString)
Parameters
name
System.String System.String
The section to render.
Returns
System.Threading.Tasks.Task<HtmlString> System.Threading.Tasks.Task(Of HtmlString)
A System.Threading.Tasks.Task<TResult> that on completion returns Empty that allows the Write(Object) call to succeed.

Remarks

The method writes to the Output and the value returned is a token value that allows the Write (produced due to @RenderSection(..)) to succeed. However the value does not represent the rendered content.

RenderSectionAsync(String, Boolean) RenderSectionAsync(String, Boolean)

In layout pages, asynchronously renders the content of the section named name.
public Task<HtmlString> RenderSectionAsync(string name, bool required)Public Function RenderSectionAsync(name As String, required As Boolean) As Task(Of HtmlString)
Parameters
name
System.String System.String
The section to render.
required
System.Boolean System.Boolean
Indicates the name section must be registered (using @section) in the page.
Returns
System.Threading.Tasks.Task<HtmlString> System.Threading.Tasks.Task(Of HtmlString)
A System.Threading.Tasks.Task<TResult> that on completion returns Empty that allows the Write(Object) call to succeed.
Exceptions
System.InvalidOperationException System.InvalidOperationException
if required is true and the section was not registered using the @section in the Razor page.

Remarks

The method writes to the Output and the value returned is a token value that allows the Write (produced due to @RenderSection(..)) to succeed. However the value does not represent the rendered content.

SetAntiforgeryCookieAndHeader() SetAntiforgeryCookieAndHeader()

Sets antiforgery cookie and X-Frame-Options header on the response.
public virtual HtmlString SetAntiforgeryCookieAndHeader()Public Overridable Function SetAntiforgeryCookieAndHeader As HtmlString
Returns

Remarks

Call this method to send antiforgery cookie token and X-Frame-Options header to client before FlushAsync() flushes the headers.

StartTagHelperWritingScope(HtmlEncoder) StartTagHelperWritingScope(HtmlEncoder)

Starts a new writing scope and optionally overrides HtmlEncoder within that scope.
public void StartTagHelperWritingScope(HtmlEncoder encoder)Public Sub StartTagHelperWritingScope(encoder As HtmlEncoder)
Parameters
encoder
System.Text.Encodings.Web.HtmlEncoder System.Text.Encodings.Web.HtmlEncoder
The System.Text.Encodings.Web.HtmlEncoder to use when this RazorPage handles non-IHtmlContent C# expressions. If null, does not change HtmlEncoder.

Remarks

All writes to the Output or Writer after calling this method will be buffered until EndTagHelperWritingScope() is called.

Write(Object) Write(Object)

Writes the specified value with HTML encoding to Output.
public virtual void Write(object value)Public Overridable Sub Write(value As Object)
Parameters
value
System.Object System.Object
The System.Object to write.

WriteAttributeValue(String, Int32, Object, Int32, Int32, Boolean) WriteAttributeValue(String, Int32, Object, Int32, Int32, Boolean)

public void WriteAttributeValue(string prefix, int prefixOffset, object value, int valueOffset, int valueLength, bool isLiteral)Public Sub WriteAttributeValue(prefix As String, prefixOffset As Integer, value As Object, valueOffset As Integer, valueLength As Integer, isLiteral As Boolean)
Parameters
prefix
System.String System.String
prefixOffset
System.Int32 System.Int32
value
System.Object System.Object
valueOffset
System.Int32 System.Int32
valueLength
System.Int32 System.Int32
isLiteral
System.Boolean System.Boolean

WriteAttributeValueTo(TextWriter, String, Int32, Object, Int32, Int32, Boolean) WriteAttributeValueTo(TextWriter, String, Int32, Object, Int32, Int32, Boolean)

public void WriteAttributeValueTo(TextWriter writer, string prefix, int prefixOffset, object value, int valueOffset, int valueLength, bool isLiteral)Public Sub WriteAttributeValueTo(writer As TextWriter, prefix As String, prefixOffset As Integer, value As Object, valueOffset As Integer, valueLength As Integer, isLiteral As Boolean)
Parameters
writer
System.IO.TextWriter System.IO.TextWriter
prefix
System.String System.String
prefixOffset
System.Int32 System.Int32
value
System.Object System.Object
valueOffset
System.Int32 System.Int32
valueLength
System.Int32 System.Int32
isLiteral
System.Boolean System.Boolean

WriteLiteral(Object) WriteLiteral(Object)

Writes the specified value without HTML encoding to Output.
public virtual void WriteLiteral(object value)Public Overridable Sub WriteLiteral(value As Object)
Parameters
value
System.Object System.Object
The System.Object to write.

WriteLiteralTo(TextWriter, Object) WriteLiteralTo(TextWriter, Object)

Writes the specified value without HTML encoding to the writer.
public virtual void WriteLiteralTo(TextWriter writer, object value)Public Overridable Sub WriteLiteralTo(writer As TextWriter, value As Object)
Parameters
writer
System.IO.TextWriter System.IO.TextWriter
The System.IO.TextWriter instance to write to.
value
System.Object System.Object
The System.Object to write.

WriteLiteralTo(TextWriter, String) WriteLiteralTo(TextWriter, String)

Writes the specified value without HTML encoding to Output.
public virtual void WriteLiteralTo(TextWriter writer, string value)Public Overridable Sub WriteLiteralTo(writer As TextWriter, value As String)
Parameters
writer
System.IO.TextWriter System.IO.TextWriter
The System.IO.TextWriter instance to write to.
value
System.String System.String
The System.String to write.

WriteTo(TextWriter, Object) WriteTo(TextWriter, Object)

Writes the specified value with HTML encoding to writer.
public virtual void WriteTo(TextWriter writer, object value)Public Overridable Sub WriteTo(writer As TextWriter, value As Object)
Parameters
writer
System.IO.TextWriter System.IO.TextWriter
The System.IO.TextWriter instance to write to.
value
System.Object System.Object
The System.Object to write.

Remarks

values of type IHtmlContent are written using Microsoft.AspNetCore.Html.IHtmlContent.WriteTo(System.IO.TextWriter, System.Text.Encodings.Web.HtmlEncoder). For all other types, the encoded result of System.Object.ToString() is written to the writer.

WriteTo(TextWriter, String) WriteTo(TextWriter, String)

Writes the specified value with HTML encoding to writer.
public virtual void WriteTo(TextWriter writer, string value)Public Overridable Sub WriteTo(writer As TextWriter, value As String)
Parameters
writer
System.IO.TextWriter System.IO.TextWriter
The System.IO.TextWriter instance to write to.
value
System.String System.String
The System.String to write.

WriteTo(TextWriter, HtmlEncoder, Object) WriteTo(TextWriter, HtmlEncoder, Object)

Writes the specified value with HTML encoding to given writer.
public static void WriteTo(TextWriter writer, HtmlEncoder encoder, object value)Public Shared Sub WriteTo(writer As TextWriter, encoder As HtmlEncoder, value As Object)
Parameters
writer
System.IO.TextWriter System.IO.TextWriter
The System.IO.TextWriter instance to write to.
encoder
System.Text.Encodings.Web.HtmlEncoder System.Text.Encodings.Web.HtmlEncoder
The System.Text.Encodings.Web.HtmlEncoder to use when encoding value.
value
System.Object System.Object
The System.Object to write.

Remarks

values of type IHtmlContent are written using Microsoft.AspNetCore.Html.IHtmlContent.WriteTo(System.IO.TextWriter, System.Text.Encodings.Web.HtmlEncoder). For all other types, the encoded result of System.Object.ToString() is written to the writer.