### Razor Markup Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SupportsRazorCommentBetweenElseIfAndElseClause.cspans.txt Illustrates basic markup rendering in Razor. No specific setup is required beyond a Razor environment. ```html Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [79] ) ``` -------------------------------- ### Razor Markup Transition Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpToMarkupSwitchTest/GivesSpacesToCodeOnInvalidAtTagTransitionInDesignTimeMode.cspans.txt Shows a transition within markup elements in Razor. No specific setup is required. ```razor Transition span at (8:1,4 [1] ) - Parent: Markup block at (8:1,4 [11] ) ``` -------------------------------- ### Install Dependencies and Launch Extension Source: https://github.com/dotnet/razor/blob/main/docs/InsertingRazorIntoVSCodeCSharp.md After updating the package.json, execute this command to install necessary dependencies. Subsequently, use the 'Launch Extension' task to validate your local changes. ```bash npx gulp installDependencies ``` -------------------------------- ### Razor Tag Content Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpToMarkupSwitchTest/GivesSpacesToCodeOnInvalidAtTagTransitionInDesignTimeMode.cspans.txt Shows content within a Razor tag. No specific setup is required. ```html Markup span at (15:1,11 [4] ) - Parent: Tag block at (15:1,11 [4] ) ``` -------------------------------- ### Razor Component Structure Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithMultipleUsingDirectives/TestComponent.ir.txt This example shows the typical using directives and the structure of a basic Razor component, including the BuildRenderTree method. ```cshtml using System.IO using Microsoft.AspNetCore.Components using System.Reflection using System.Collections.Generic using System.Linq using System.Threading.Tasks namespace Test public partial class TestComponent : ComponentBase { protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) { ;; } } ``` -------------------------------- ### Razor Component Rendering - Start Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/RazorTemplate_NonGeneric_InImplicitExpression/TestComponent.mappings.txt Represents the start of a component's rendering logic in Razor. ```cshtml RenderPerson( ``` -------------------------------- ### Razor Markup Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpTemplateTest/HandlesTwoTemplatesInStatementWithinCodeBlock.cspans.txt Demonstrates basic markup rendering in Razor. ```cshtml Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [91] ) ``` -------------------------------- ### Razor Nested Markup Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpToMarkupSwitchTest/GivesSpacesToCodeOnInvalidAtTagTransitionInDesignTimeMode.cspans.txt Illustrates nested markup structures within Razor. No specific setup is required. ```html Markup span at (12:1,8 [3] ) - Parent: Markup block at (8:1,4 [11] ) ``` -------------------------------- ### Razor Markup Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperBlockRewriterTest/UnderstandsMinimizedAttributes_Block31.cspans.txt Illustrates basic markup usage in Razor. ```html Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [46] ) ``` -------------------------------- ### Example Azure Pipelines YAML with Multiple Outputs Source: https://github.com/dotnet/razor/blob/main/eng/common/template-guidance.md This example demonstrates how to configure multiple outputs for artifact publishing within an 1ES managed pipeline. Ensure publish artifacts are located in `$(Build.ArtifactStagingDirectory)` and utilize the 1ES provided template context to reduce security scans. ```yaml extends: template: azure-pipelines/MicroBuild.1ES.Official.yml@MicroBuildTemplate parameters: stages: - stage: build jobs: - template: /eng/common/templates-official/jobs/jobs.yml@self parameters: # 1ES makes use of outputs to reduce security task injection overhead templateContext: outputs: - output: pipelineArtifact displayName: 'Publish logs from source' continueOnError: true condition: always() targetPath: $(Build.ArtifactStagingDirectory)/artifacts/log artifactName: Logs jobs: - job: Windows steps: - script: echo "friendly neighborhood" > artifacts/marvel/spiderman.txt # copy build outputs to artifact staging directory for publishing - task: CopyFiles@2 displayName: Gather build output inputs: SourceFolder: '$(System.DefaultWorkingDirectory)/artifacts/marvel' Contents: '**' TargetFolder: '$(Build.ArtifactStagingDirectory)/artifacts/marvel' ``` -------------------------------- ### Razor Markup Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperBlockRewriterTest/GeneratesExpectedOutputForUnboundDataDashAttributes_Block4.cspans.txt Illustrates basic markup within a Razor file. ```razor Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [49] ) ``` -------------------------------- ### BodyTagHelper Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ModelExpressionTagHelper_DesignTime.ir.txt Shows an example of the BodyTagHelper, which can be used to wrap content within the HTML body tag. This is a standard ASP.NET Core Razor Tag Helper. ```cshtml ``` -------------------------------- ### Razor HTML Helper Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/LocalFunctionsWithGenerics.cspans.txt Razor can render HTML using helpers. This example shows a simple paragraph. ```cshtml

This is a paragraph rendered by Razor.

``` -------------------------------- ### Define a RenderTreeBuilder Method Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Legacy_3_1_WhiteSpace_InMarkupInFunctionsBlock/TestComponent.mappings.txt Example of defining a method that accepts a RenderTreeBuilder. ```csharp void MyMethod(RenderTreeBuilder __builder) { ``` -------------------------------- ### Razor Markup Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/RazorDirectivesTest/TypeParam_WithSemicolon.cspans.txt Illustrates basic Razor markup. This content is rendered directly. ```razor @{ // Code block }

This is markup.

@SomeVariable @SomeMethod() ``` -------------------------------- ### Razor Empty Tag Block Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/HtmlAttributeTest/ConditionalAttributesAreDisabledForDataAttributesInDocument.cspans.txt An example of an empty tag block in Razor syntax. It is defined by its start and end spans and does not contain any content. ```razor Markup span at (23:0,23 [1] ) (Accepts:None) - Parent: Tag block at (2:0,2 [22] ) ``` ```razor Markup span at (24:0,24 [7] ) (Accepts:None) - Parent: Tag block at (24:0,24 [7] ) ``` -------------------------------- ### Razor Transition Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/HtmlTagsTest/VoidElementFollowedByCloseTag.cspans.txt Shows how Razor transitions between code and markup. The '@' symbol is key for these transitions. ```razor @if (DateTime.Now.DayOfWeek == DayOfWeek.Friday) {

It's Friday!

} else {

It's not Friday.

} ``` -------------------------------- ### List All Available Benchmarks Source: https://github.com/dotnet/razor/blob/main/src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/README.md Run the benchmark executable without any parameters to view a list of all available benchmarks and select one to execute. Compile in Release mode and set the target framework. ```cmd dotnet run -c Release -f net472 ``` -------------------------------- ### Razor Expression Transition Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/HtmlAttributeTest/MultiValueExpressionAttribute.cspans.txt This indicates a transition into an expression block, typically starting with '@'. ```razor Transition span at (20:0,20 [1] ) (Accepts:None) - Parent: Expression block at (20:0,20 [4] ) ``` -------------------------------- ### Razor Tag Helper Start Tag Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperBlockRewriterTest/UnderstandsMinimizedAttributes_Block27.stree.txt The starting tag of a Razor Tag Helper, including its name and attributes. This example defines a paragraph with a bound integer attribute and a class attribute. ```Razor

``` -------------------------------- ### List All Benchmarks Source: https://github.com/dotnet/razor/blob/main/src/Compiler/perf/Microbenchmarks/readme.md Compile the solution in Release mode and run without parameters to see a list of all available benchmarks and choose one to execute. ```bash dotnet run -c Release ``` -------------------------------- ### RazorDocument Structure Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/CorrectlyParsesDoWhileBlockMissingWhileClauseEntirely.stree.txt Illustrates the parsing of a RazorDocument, showing the breakdown into MarkupTextLiteral and CSharpCodeBlock elements. This example highlights the transition from markup to code and back. ```razor @do { var foo = bar; } narf; ``` -------------------------------- ### Initialize Dictionary Object Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithEditorRequiredParameter_ValuesSpecifiedUsingSplatting/TestComponent.mappings.txt Shows how to initialize a new dictionary with string keys and object values. ```csharp new Dictionary() ``` -------------------------------- ### Razor Expression Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpImplicitExpressionTest/ParsesNullForgivenessOperatorImplicitExpression_DirectiveCodeBlock.cspans.txt Demonstrates how to render dynamic content using Razor expressions. ```cshtml @DateTime.Now.Year ``` -------------------------------- ### Start Specific Visual Studio Instance (Windows) Source: https://context7.com/dotnet/razor/llms.txt Provides an option to choose and launch a specific Visual Studio instance with the Razor solution loaded. ```bash .\startvs.cmd -chooseVS ``` -------------------------------- ### Razor Tag Markup Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpToMarkupSwitchTest/GivesSpacesToCodeOnInvalidAtTagTransitionInDesignTimeMode.cspans.txt Represents markup within a Razor tag. No specific setup is required. ```html Markup span at (9:1,5 [3] ) - Parent: Tag block at (9:1,5 [3] ) ``` -------------------------------- ### Razor MetaCode Statement Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpToMarkupSwitchTest/GivesSpacesToCodeOnInvalidAtTagTransitionInDesignTimeMode.cspans.txt Represents meta-code within a Razor statement block. No specific setup is required. ```razor MetaCode span at (25:2,0 [1] ) - Parent: Statement block at (0:0,0 [26] ) ``` -------------------------------- ### RazorProjectEngine.Create Source: https://context7.com/dotnet/razor/llms.txt Demonstrates how to create and configure a RazorProjectEngine to compile Razor files, including setting root namespaces, C# language versions, and registering extensions. ```APIDOC ## RazorProjectEngine.Create ### Description The `RazorProjectEngine` is the main entry point for programmatically compiling Razor files. It provides configuration options for the compiler, registers directives and features, and processes Razor source files into C# code documents. ### Method `RazorProjectEngine.Create()` ### Endpoint N/A (Programmatic API) ### Parameters None directly for `Create()`, but configuration is passed via builder. #### Request Body N/A ### Request Example ```csharp using Microsoft.AspNetCore.Razor.Language; using System.IO; // Create a basic Razor project engine with default configuration var fileSystem = RazorProjectFileSystem.Create("/path/to/project"); var configuration = RazorConfiguration.Default; var projectEngine = RazorProjectEngine.Create(configuration, fileSystem, builder => { // Set the root namespace for generated code builder.SetRootNamespace("MyApp.Views"); // Configure C# language version builder.SetCSharpLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp12); // Register MVC extensions based on configuration builder.RegisterExtensions(); }); // Process a Razor file var projectItem = fileSystem.GetItem("/Views/Home/Index.cshtml", FileKinds.Legacy); var codeDocument = projectEngine.Process(projectItem); // Get the generated C# code var csharpDocument = codeDocument.GetRequiredCSharpDocument(); Console.WriteLine(csharpDocument.Text); // Check for compilation diagnostics foreach (var diagnostic in csharpDocument.Diagnostics) { Console.WriteLine($"{diagnostic.Severity}: {diagnostic.GetMessage()}"); } ``` ### Response #### Success Response (200) - **csharpDocument.Text** (string) - The generated C# code from the Razor file. - **csharpDocument.Diagnostics** (IEnumerable) - A collection of compilation diagnostics. #### Response Example ```json // Output of csharpDocument.Text will be C# code // Output of diagnostics will be severity and message strings ``` ``` -------------------------------- ### Razor Expression Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpPreprocessorTest/ElseNotOnNewline.cspans.txt Razor code expressions start with @ followed by a C# expression. The result of the expression is written to the output. ```razor

@message

``` -------------------------------- ### Restore Dependencies and Build Project Source: https://github.com/dotnet/razor/blob/main/docs/contributing/BuildFromSource.md Run this script to download necessary tools and build the entire Razor repository. It's recommended to run this after pulling significant changes. ```powershell .\restore.cmd ``` -------------------------------- ### Razor Loop Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpBlockTest/ParsersCanNestRecursively.cspans.txt Iterate over a collection and render HTML for each item. ```html ``` -------------------------------- ### Razor Tag Helper Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/HtmlTagsTest/TextTags.cspans.txt Demonstrates the use of tag helpers within Razor, which transform HTML tags into server-side logic. This example shows a tag helper in action. ```html Transition span at (11:0,11 [7] ) - Parent: Tag block at (11:0,11 [7] ) ``` -------------------------------- ### Razor Tag Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpSectionTest/HandlesUnterminatedSectionWithNestedIf.cspans.txt Illustrates a Razor tag. ```cshtml

Tag block

``` -------------------------------- ### Start Visual Studio with Roslyn Dependencies (Windows) Source: https://context7.com/dotnet/razor/llms.txt Launches Visual Studio with the Razor solution, including Roslyn dependencies for debugging purposes. ```bash .\startvs.cmd -includeRoslynDeps ``` -------------------------------- ### Razor Directive Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/HtmlBlockTest/TerminatesAtEOF.cspans.txt Razor directives start with '@' followed by a directive name. They are used to configure the Razor page or view. ```csharp @page "/MyPage" @using System.Globalization ``` -------------------------------- ### Razor Markup Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/RazorDirectivesTest/TypeParam_WithoutSemicolon.cspans.txt Shows basic HTML markup within a Razor file. ```cshtml

This is static HTML.

``` -------------------------------- ### Razor Expression Block Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperBlockRewriterTest/GeneratesExpectedOutputForUnboundDataDashAttributes_Block4.cspans.txt Demonstrates an expression block in Razor syntax. ```razor Transition span at (24:0,24 [1] ) - Parent: Expression block at (24:0,24 [13] ) ``` ```razor Code span at (25:0,25 [12] ) - Parent: Expression block at (24:0,24 [13] ) ``` -------------------------------- ### Configure Razor Compiler with RazorConfiguration Source: https://context7.com/dotnet/razor/llms.txt Demonstrates creating and using RazorConfiguration objects for different scenarios like .NET 8 Blazor and MVC. Shows how to access configuration details from a RazorProjectEngine. ```csharp using Microsoft.AspNetCore.Razor.Language; using Microsoft.CodeAnalysis.CSharp; using System.Collections.Immutable; // Default configuration var defaultConfig = RazorConfiguration.Default; Console.WriteLine($ ``` -------------------------------- ### Razor Expression Block Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/HtmlAttributeTest/MultiValueExpressionAttribute.cspans.txt Razor expression blocks are used to render C# expressions directly into the output. They start with '@'. ```razor Transition span at (11:0,11 [1] ) (Accepts:None) - Parent: Expression block at (11:0,11 [4] ) ``` -------------------------------- ### Razor Expression Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpVerbatimBlockTest/InnerImplicitExprWithOnlySingleAtAcceptsSingleSpaceOrNewlineAtDesignTime.cspans.txt Razor expressions, starting with '@', evaluate a C# expression and render its result as a string. They are typically single-line. ```csharp Transition span at (8:1,4 [1] ) (Accepts:None) - Parent: Expression block at (8:1,4 [1] ) Code span at (9:1,5 [0] ) (Accepts:NonWhitespace) - Parent: Expression block at (8:1,4 [1] ) ``` -------------------------------- ### Razor Syntax Examples Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/AddAttribute_ImplicitStringConversion_CustomEvent/TestComponent.mappings.txt Illustrates various Razor syntax elements including parameters, events, and a simple lambda expression. ```cshtml c ``` ```cshtml () => { } ``` ```cshtml true ``` ```cshtml MyParameter ``` ```cshtml MyEvent ``` ```cshtml BoolParameter ``` ```cshtml StringParameter ``` ```cshtml DelegateParameter ``` ```cshtml ObjectParameter ``` -------------------------------- ### Razor Document Structure Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/EscapedIdentifiers_02.stree.txt Illustrates a basic Razor document structure containing C# code interspersed with markup. This example shows how Razor handles string formatting and variable declarations within a Razor template. ```Razor @{ @string.Format("1{0}", DateTime.Now) var x = 1; var y = @x; @string.Format("2{0}", DateTime.Now) } ``` -------------------------------- ### Razor Directive Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/HtmlToCodeSwitchTest/GivesWhitespacePreceedingToCodeIfThereIsNoMarkupOnThatLine.cspans.txt Applying Razor directives for global settings or imports. ```html @page @model MyModel @using System.Globalization ``` -------------------------------- ### Razor Markup Span Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpToMarkupSwitchTest/GivesSpacesToCodeOnInvalidAtTagTransitionInDesignTimeMode.cspans.txt Represents plain HTML or text within a Razor file. No specific setup is required. ```html Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [26] ) ``` -------------------------------- ### Run a Specific Benchmark Source: https://github.com/dotnet/razor/blob/main/src/Compiler/perf/Microbenchmarks/readme.md Compile the solution in Release mode and execute a specific benchmark by providing its name as a parameter. ```bash dotnet run -c Release ``` -------------------------------- ### Razor Document Structure Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpToMarkupSwitchTest/CodeBlocksTrailingWhitespace_06.stree.txt This example shows a basic Razor document containing HTML markup and a C# code block with a directive. It highlights the parsing of Razor syntax into distinct blocks. ```razor @code {
} ``` -------------------------------- ### Build Razor Project on Windows Source: https://github.com/dotnet/razor/blob/main/docs/contributing/BuildFromSource.md Execute this command to build the entire Razor project from the command line on a Windows system. ```powershell .\build.cmd ``` -------------------------------- ### Form TagHelper Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.ir.txt Illustrates the use of a FormTagHelper. This snippet shows basic form element rendering. ```cshtml
``` -------------------------------- ### Razor Transition Span Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/HtmlDocumentTest/OpenAngle_09.cspans.txt Indicates the start of a Razor expression or code block. It signals a shift from markup to executable code. ```razor Transition span at (2:0,2 [1] ) - Parent: Expression block at (2:0,2 [6] ) ``` -------------------------------- ### Razor Statement Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/HtmlBlockTest/TerminatesAtEOF.cspans.txt Razor statements start with '@' followed by a code block. They are used for control flow and logic within Razor views. ```csharp @{ var message = "Hello from Razor statement!";

@message

} ``` -------------------------------- ### Input Tag Helper Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.ir.txt Shows how to use the built-in Input Tag Helper with custom attributes. ```cshtml ``` -------------------------------- ### Razor Conditional Compilation with #endif Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimePreprocessorDirectiveTest/StartOfLine_03/TestComponent.mappings.txt The #endif directive marks the end of a conditional compilation block started with #if. This example shows the closing part of a conditional block. ```razor var y = 2; #endif ``` -------------------------------- ### Razor MetaCode Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpFunctionsTest/MarkupInFunctionsBlock_MarkupCanContainTemplate.cspans.txt Example of MetaCode within a Razor directive block. ```cshtml @{ // MetaCode comment } ``` -------------------------------- ### Run a Specific Benchmark Source: https://github.com/dotnet/razor/blob/main/src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/README.md Execute a particular benchmark by specifying its name as a parameter. Ensure the solution is compiled in Release mode and the target framework is set. ```cmd dotnet run -c Release -f net472 ``` -------------------------------- ### Razor Form Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/HtmlTagsTest/VoidElementFollowedByCloseTag.cspans.txt Illustrates creating an HTML form using Razor syntax and Tag Helpers. ```html
``` -------------------------------- ### Boolean Value Example (False) Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericInference_DynamicallyAccessedMembers_01/TestComponent.mappings.txt Example of a string literal representing a boolean false value. ```cshtml "false" ``` -------------------------------- ### Razor Code Block Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpBlockTest/ParsersCanNestRecursively.cspans.txt Demonstrates a multi-line code block in Razor syntax. ```html @{ var greeting = "Welcome!";

@greeting

} ``` -------------------------------- ### Boolean Value Example (True) Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericInference_DynamicallyAccessedMembers_01/TestComponent.mappings.txt Example of a string literal representing a boolean true value. ```cshtml "true" ``` -------------------------------- ### Razor Transition Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpBlockTest/CSharpBlock_SingleLineControlFlowStatement_Error.cspans.txt Shows the transition syntax in Razor, used to switch between markup and code contexts. ```cshtml @* This is a Razor comment *@

This is markup.

@{ // This is a code block } @DateTime.Now ``` -------------------------------- ### Razor Comment Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpRazorCommentsTest/RazorCommentBetweenCodeBlockAndMarkup.stree.txt Illustrates how comments are structured within a Razor file. ```razor /* Hello World */ ``` -------------------------------- ### Razor Markup Block Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpTemplateTest/HandlesTwoTemplatesInStatementWithinCodeBlock.cspans.txt Illustrates a Razor markup block. ```cshtml Transition span at (49:0,49 [1] ) - Parent: Markup block at (49:0,49 [18] ) ``` ```cshtml Markup span at (53:0,53 [5] ) - Parent: Markup block at (49:0,49 [18] ) ``` ```cshtml Transition span at (69:0,69 [1] ) - Parent: Markup block at (69:0,69 [18] ) ``` ```cshtml Markup span at (73:0,73 [5] ) - Parent: Markup block at (69:0,69 [18] ) ``` -------------------------------- ### Using InputRadioGroup Component Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericInference_DynamicallyAccessedMembers_01/TestComponent.mappings.txt Demonstrates the basic usage of the InputRadioGroup component. Ensure the `Microsoft.AspNetCore.Components.Forms` namespace is imported. ```cshtml InputRadioGroup ``` -------------------------------- ### Start Polling for Sync Status Source: https://github.com/dotnet/razor/blob/main/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/TestFiles/FormattingLog/GameTracAdmin/InitialDocument.txt Starts a timer to poll for sync status updates. It ensures any existing polling timer is stopped before starting a new one. This method is typically called when a sync is initiated or detected as running. ```csharp private void StartPolling() { StopPolling(); ``` -------------------------------- ### Razor Markup Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/EscapedIdentifiers_09.cspans.txt Basic markup within a Razor file. ```html @{ // Code here } ``` -------------------------------- ### Razor Directive Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/RazorDirectivesTest/ExtensibleDirectiveDoesNotErorrIfNotAtStartOfLineBecauseOfWhitespace.cspans.txt An example of a Razor directive, commonly used for page-level configuration or model binding. ```cshtml @using System.Globalization @inject System.Globalization.CultureInfo Culture ``` -------------------------------- ### Basic Razor Syntax Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/AddAttribute_ImplicitStringConversion_CustomEvent/TestComponent.mappings.txt Illustrates basic Razor syntax elements like component names and parameters. ```razor c ``` ```razor true ``` ```razor MyComponent ``` ```razor MyParameter ``` ```razor MyEvent ``` ```razor BoolParameter ``` ```razor StringParameter ``` ```razor DelegateParameter ``` ```razor ObjectParameter ``` -------------------------------- ### Basic Razor Component Usage Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/DuplicateComponentParameters_IsAnError_BindMessage/TestComponent.mappings.txt Demonstrates the basic structure and rendering of a Razor component. No specific setup is required beyond a standard .NET project with Razor support. ```cshtml @{ string message = "hi"; } ``` -------------------------------- ### Build Razor Project on macOS/Linux Source: https://github.com/dotnet/razor/blob/main/docs/contributing/BuildFromSource.md Execute this command to build the entire Razor project from the command line on a macOS or Linux system. ```bash ./build.sh ``` -------------------------------- ### Razor Event Handler Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/EventCallbackOfT_GenericComponent_ExplicitType/TestComponent.mappings.txt Example of an event handler in Razor syntax. This is typically used for user interactions. ```razor @OnClick ``` -------------------------------- ### Run Tests with Build Script (macOS/Linux) Source: https://github.com/dotnet/razor/blob/main/docs/contributing/BuildFromSource.md Execute the build script with the --test option to run tests on macOS/Linux. ```bash ./build.sh --test ``` -------------------------------- ### Minimized Bound Attribute Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SymbolBoundAttributes_DesignTime.ir.txt Illustrates a minimized bound HTML attribute. This is a general example of attribute binding. ```cshtml ``` -------------------------------- ### Razor Statement Block Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpStatementTest/SwitchStatement_ListPattern.cspans.txt A block containing Razor statements. This example shows a parent statement block. ```razor Statement block at (2:1,0 [165] ) ``` -------------------------------- ### Using System.Text Directive Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Usings_OutOfOrder_DesignTime.mappings.txt Demonstrates the generation of the 'using System.Text;' directive. ```cshtml using System.Text ``` -------------------------------- ### Razor Markup Block Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpStatementTest/SwitchStatement_ListPattern.cspans.txt A block containing markup content. This example shows a nested markup block. ```razor Markup block at (49:4,0 [99] ) ``` -------------------------------- ### Razor Comment Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/RazorDirectivesTest/DirectiveDescriptor_FileScoped_CanBeBeneathOtherWhiteSpaceCommentsAndDirectives.cspans.txt Shows how to include comments in Razor files, which are ignored during rendering. ```razor @* This is a Razor comment *@

This content is visible.

``` -------------------------------- ### Razor Tag Block Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpStatementTest/SwitchStatement_ListPattern.cspans.txt Represents a tag within Razor syntax. This example shows a parent tag block. ```razor Tag block at (57:4,8 [8] ) ``` ```razor Tag block at (137:4,88 [9] ) ``` -------------------------------- ### Markup Start Tag in Razor Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpImplicitExpressionTest/ParsesNullForgivenessOperatorImplicitExpression_IncompleteBracketWithHtml.stree.txt Represents the start tag of an HTML element in Razor, such as '

'. It is parsed and handled by the SpanEditHandler. ```Razor MarkupStartTag - [6..9)::3 - [

] - Gen - SpanEditHandler;Accepts:Any ``` -------------------------------- ### Razor Expression Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/RazorDirectivesTest/TypeParam_WithSemicolon.cspans.txt Embed C# expressions within Razor markup using the `@` symbol. This example displays a string variable. ```cshtml

Hello, @name!

``` -------------------------------- ### Razor MetaCode Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/EscapedIdentifiers_09.cspans.txt Illustrates the use of MetaCode within Razor for specific directives or constructs. ```csharp @{ // MetaCode example } ``` -------------------------------- ### Razor Directive Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpFunctionsTest/MarkupInFunctionsBlock_CanContainCurlyBraces.cspans.txt An example of a Razor directive, specifically `@using`, which imports a namespace. This is typically placed at the top of a Razor file. ```cshtml @using System.Web.Mvc ``` -------------------------------- ### Open Visual Studio with Razor Solution Source: https://github.com/dotnet/razor/blob/main/docs/contributing/BuildFromSource.md Use this script to launch Visual Studio with the Razor solution, after setting required environment variables. Optional switches can be used to choose a VS instance or include Roslyn dependencies. ```powershell .\startvs.cmd ``` -------------------------------- ### Basic Input Tag Helper Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicTagHelpers_DesignTime.ir.txt Demonstrates the use of the input tag helper with different types and attributes. This is useful for generating standard HTML input elements. ```cshtml ``` ```cshtml ``` -------------------------------- ### Razor Component Value Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_GenericBind_TypeInference/TestComponent.mappings.txt Demonstrates a simple value assignment within a Razor component. This is a basic example of how values are handled. ```cshtml string Value; ``` -------------------------------- ### Razor Tag Content Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpTemplateTest/HandlesSimpleTemplateInStatementWithinStatementBlock.cspans.txt This example shows markup content within a Razor tag block, which is processed by the Razor engine. ```razor Markup span at (58:0,58 [4] ) (Accepts:None) - Parent: Tag block at (58:0,58 [4] ) ``` -------------------------------- ### Clone Razor Repository Source: https://github.com/dotnet/razor/blob/main/docs/contributing/BuildFromSource.md Use this command to get a fresh copy of the Razor project from GitHub. ```powershell git clone https://github.com/dotnet/razor.git ``` -------------------------------- ### Razor Method Call Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ChildComponent_GenericChildContent/TestComponent.mappings.txt Illustrates calling a method within Razor syntax. Ensure the context object is available. ```cshtml context.ToLower() ``` -------------------------------- ### UrlResolutionTagHelper Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ModelExpressionTagHelper_DesignTime.ir.txt Example of using the UrlResolutionTagHelper to resolve URLs within your Razor views. This helper is useful for generating correct paths for assets. ```cshtml ``` -------------------------------- ### Razor Component Syntax Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpBlockTest/ParsersCanNestRecursively.cspans.txt Illustrates basic component rendering in Razor. ```html ``` -------------------------------- ### Use RazorConfiguration with RazorProjectEngine Source: https://context7.com/dotnet/razor/llms.txt Demonstrates how to create a RazorProjectEngine using a specific RazorConfiguration and access its properties. This is useful for integrating custom Razor compilation settings into your project. ```csharp var projectEngine = RazorProjectEngine.Create( blazorConfig, RazorProjectFileSystem.Empty, builder => { builder.RegisterExtensions(); // Registers MVC extensions based on ConfigurationName builder.SetRootNamespace("MyApp"); }); // Access configuration from engine Console.WriteLine($"Using: {projectEngine.Configuration.ConfigurationName}"); Console.WriteLine($"Razor: {projectEngine.Configuration.LanguageVersion}"); Console.WriteLine($"C#: {projectEngine.Configuration.CSharpLanguageVersion}"); ``` -------------------------------- ### Razor Code Snippet Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/TagHelpersInSection_DesignTime.mappings.txt A basic example of embedding C# code within a Razor view. This snippet assigns a string value to a variable. ```cshtml var code = "some code"; ``` -------------------------------- ### Restore Dependencies and Build (Windows) Source: https://context7.com/dotnet/razor/llms.txt Restores project dependencies and builds the Razor solution on Windows using batch scripts. ```bash .\restore.cmd .\build.cmd ``` -------------------------------- ### Razor Tag Block Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpSectionTest/HandlesUnterminatedSectionWithNestedIf.cspans.txt Tag blocks in Razor are used for specific HTML tag processing. This example shows an empty tag block. ```html

``` -------------------------------- ### Run Razor Syntax Generator Source: https://github.com/dotnet/razor/blob/main/src/Compiler/tools/RazorSyntaxGenerator/README.md Execute the syntax generator tool from the command line. Provide the full path to the Syntax.xml file and the desired output directory. ```bash dotnet run `full/path/to/Syntax.xml` `full/path/to/generated/output` ``` -------------------------------- ### Razor Tag Block Markup Example 2 Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpPreprocessorTest/IfDefNotOnNewline_02.cspans.txt Another example of markup within a Razor tag block, indicating content that is rendered directly. ```razor Markup span at (26:2,4 [3] ) (Accepts:Any) - Parent: Tag block at (26:2,4 [3] ) ``` -------------------------------- ### Razor Tokenization Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpBlockTest/ParsesMultipleElseIfBranchesOfIfStatementFollowedByOneElseBranch.stree.txt Demonstrates the tokenization of a Razor code block, showing how different elements like identifiers, operators, and literals are recognized. ```text Identifier;[Bar]; Whitespace;[ ]; Assign;[=]; Whitespace;[ ]; StringLiteral;["baz"]; Whitespace;[ ]; RightBrace;[}]; RightParenthesis;[)]; Whitespace;[ ]; LeftBrace;[{]; NewLine;[LF]; Whitespace;[ ]; Identifier;[Debug]; Dot;[.]; Identifier;[WriteLine]; LeftParenthesis;[(]; StringLiteral;[@"bar } baz"]; RightParenthesis;[)]; Semicolon;[;]; NewLine;[LF]; RightBrace;[}]; Whitespace;[ ]; Keyword;[else]; Whitespace;[ ]; LeftBrace;[{]; Whitespace;[ ]; Identifier;[Debug]; Dot;[.]; Identifier;[WriteLine]; LeftParenthesis;[(]; StringLiteral;[@"bar } baz"]; RightParenthesis;[)]; Semicolon;[;]; Whitespace;[ ]; RightBrace;[}] ``` -------------------------------- ### Razor HTML Helper Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpErrorTest/RequiresControlFlowStatementsToHaveBraces.cspans.txt An example of using an HTML helper in Razor. HTML helpers generate HTML markup, often with dynamic content. ```cshtml @Html.ActionLink("Click Here", "Index", "Home") ``` -------------------------------- ### Two-way Binding with bind:get Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/BindToElement_MixingBindValueWithGetSet/TestComponent.ir.txt Use `bind:get` for two-way data binding on input elements. Ensure the bound property is correctly formatted. ```razor
``` -------------------------------- ### Input TagHelper Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.ir.txt Demonstrates the use of an InputTagHelper with self-closing tag mode. It includes properties for 'date' and 'FooProp'. ```cshtml ``` -------------------------------- ### Input Tag Helper with Dictionary Properties Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/PrefixedAttributeTagHelpers_Runtime.ir.txt Demonstrates the usage of the 'input' Tag Helper with predefined integer dictionary properties. This example shows how to bind integer dictionary values to Tag Helper attributes. ```cshtml int-prefix-value - System.Collections.Generic.IDictionary TestNamespace.InputTagHelper1.IntDictionaryProperty - HtmlAttributeValueStyle.DoubleQuotes ``` ```cshtml int-prefix-value - int TestNamespace.InputTagHelper2.IntDictionaryProperty - HtmlAttributeValueStyle.DoubleQuotes ``` -------------------------------- ### Razor Expression Block Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpTemplateTest/HandlesSimpleTemplateInStatementWithinCodeBlock.cspans.txt An example of a Razor expression block, used to render the result of a C# expression. The expression's output is written to the response. ```csharp Code span at (59:0,59 [4] ) (Accepts:NonWhitespace) - Parent: Expression block at (58:0,58 [5] ) ``` -------------------------------- ### Equipment Linking Example IC Tag Source: https://github.com/dotnet/razor/blob/main/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/TestFiles/FormattingLog/GameTracAdmin/InitialDocument.txt Provides an example of a GameTrac IC Tag format (e.g., 216000006A00) used for identifying machines in the equipment linking process. ```html 216000006A00 ``` -------------------------------- ### Render a Basic Component Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/EventCallback_CanPassEventCallback_Implicitly_FuncOfobjectTask/TestComponent.mappings.txt This snippet shows how to render a simple Razor component. No specific setup is required beyond defining the component. ```cshtml @{ var myComponent = "MyComponent"; } <@myComponent /> ``` -------------------------------- ### Razor Component Tag Helper Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperBlockRewriterTest/TagHelperParseTreeRewriter_RewritesComplexTagHelperTagBlocks8.cspans.txt An example of a Tag Helper used within a Razor component. Tag Helpers provide server-side logic for HTML elements. ```html ``` -------------------------------- ### Razor Component BuildRenderTree Conditional Compilation Start Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimePreprocessorDirectiveTest/StartOfLine_06/TestComponent.ir.txt This snippet represents the start of conditional compilation within a Razor component's BuildRenderTree method. It uses the #if directive. ```csharp #if true ``` -------------------------------- ### Run Integration Tests from Command Line Source: https://github.com/dotnet/razor/blob/main/src/Razor/test/Microsoft.VisualStudioCode.Razor.IntegrationTests/README.md Navigate to the integration test project directory and execute tests using the dotnet CLI. Ensure you are in the correct directory before running. ```powershell cd src\Razor\test\Microsoft.VisualStudioCode.Razor.IntegrationTests dotnet test ``` -------------------------------- ### Razor While Loop Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpNestedStatementsTest/NestedImplicitExpression.stree.txt Demonstrates a basic while loop within a Razor file, including C# code blocks and Razor transitions. ```razor @while(true) { @foo } ``` -------------------------------- ### Razor Markup Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/HtmlToCodeSwitchTest/ParsesCodeWithinSingleLineMarkup.cspans.txt Demonstrates basic markup within a Razor file. This is the default content type. ```razor Markup span at (0:0,0 [0] ) (Accepts:Any) - Parent: Markup block at (0:0,0 [27] ) ``` -------------------------------- ### Instantiate Placeholder in Razor Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ImplicitStringConversion_ParameterCasing_AddComponentParameter/TestComponent.mappings.txt Shows how to use the Placeholder component in a Razor file. ```cshtml ``` -------------------------------- ### Razor User Mention with HTML Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpBlockTest/CSharpBlock_SingleLineControlFlowStatement_Error.stree.txt Illustrates how to render a user mention along with HTML elements within a Razor statement. This example includes basic HTML formatting. ```cshtml @SomeGitHubUserName Hi! ``` -------------------------------- ### Basic HTML with Tag Helpers Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/AttributeTargetingTagHelpers_DesignTime.ir.txt Illustrates how Razor processes HTML content and Tag Helpers. This example shows a simple HTML structure with a Tag Helper applied. ```html

Hello World

``` ```html ``` -------------------------------- ### Preprocessor directives not at start of line Source: https://github.com/dotnet/razor/blob/main/docs/Compiler Breaking Changes - DotNet 9.md Preprocessor directives like #if must start at the beginning of a line (only whitespace allowed before them) in Razor files. Previously, this was allowed and would trigger RZ1043. ```razor @{ #if DEBUG /* Previously allowed, now triggers RZ1043 */ }
test
@{ #endif /* Previously allowed, now triggers RZ1043 */ } ``` ```razor @{ #if DEBUG /* This is allowed */ }
test
@{ #endif /* This is allowed */ } ``` -------------------------------- ### Initialize Dictionary in Razor Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/CascadingGenericInference_GenericArgumentNested_Dictionary_03/TestComponent.mappings.txt Demonstrates how to initialize a new Dictionary with a specified key and string value type in Razor syntax. ```cshtml new Dictionary() ``` -------------------------------- ### Switch Case Start in Razor Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Blocks_DesignTime.mappings.txt Initiates a switch statement, evaluating an expression ('i') and executing code based on matching case values. This snippet defines the start of the switch and the first case. ```cshtml switch(i) { case 11: ``` -------------------------------- ### Razor C# Code Block Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/HtmlToCodeSwitchTest/SectionContextGivesWhitespacePreceedingToCodeIfThereIsNoMarkupOnThatLine.stree.txt Illustrates a C# code block within Razor syntax. This example shows a closing brace and newline, typically used to end a code section. ```razor } ``` -------------------------------- ### Instantiate Dictionary in Razor Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/CascadingGenericInference_GenericArgumentNested_Dictionary_02/TestComponent.mappings.txt Demonstrates how to instantiate a new Dictionary with a specified key and value type within Razor syntax. ```cshtml () => new Dictionary() ``` -------------------------------- ### Razor Document Structure Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpPreprocessorTest/IfDefAcrossMultipleBlocks.stree.txt Illustrates the parsing of a Razor document containing conditional C# code and HTML markup. This example shows a disabled C# block within a Razor file. ```razor @{ if (false) { var x = 1; }

Content

@{ var y = 2; #endif } ``` -------------------------------- ### Razor Layout Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpPreprocessorTest/ElseNotOnNewline.cspans.txt Shows how to define and use a layout in Razor for consistent page structure. Layouts centralize common elements like headers, footers, and navigation. ```cshtml @layout MyLayout.cshtml

Page Content

``` -------------------------------- ### Razor Component Event Handling Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/EventHandler_WithDelegate_PreventDefault/TestComponent.mappings.txt A combined example showing how to handle focus events and prevent default actions within a Razor Component. Ensure the necessary using directives are present. ```cshtml void OnFocus(FocusEventArgs e) { } bool ShouldPreventDefault() { return false; } ``` -------------------------------- ### Instantiate MyComponent in Razor Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef/TestComponent.mappings.txt Shows how to instantiate a component named MyComponent within a Razor file. ```razor ``` -------------------------------- ### Razor Document Structure Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/HtmlToCodeSwitchTest/SupportsCodeWithinSGMLDeclaration.stree.txt Illustrates the parsing of a Razor document containing HTML-like markup and C# expressions. This example shows a Razor document with a root element and embedded C# code. ```razor ``` -------------------------------- ### Create RazorProjectEngine Source: https://context7.com/dotnet/razor/llms.txt Use RazorProjectEngine.Create to programmatically compile Razor files. Configure options like root namespace and C# language version. Processes Razor files into C# code and checks for diagnostics. ```csharp using Microsoft.AspNetCore.Razor.Language; using System.IO; // Create a basic Razor project engine with default configuration var fileSystem = RazorProjectFileSystem.Create("/path/to/project"); var configuration = RazorConfiguration.Default; var projectEngine = RazorProjectEngine.Create(configuration, fileSystem, builder => { // Set the root namespace for generated code builder.SetRootNamespace("MyApp.Views"); // Configure C# language version builder.SetCSharpLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp12); // Register MVC extensions based on configuration builder.RegisterExtensions(); }); // Process a Razor file var projectItem = fileSystem.GetItem("/Views/Home/Index.cshtml", FileKinds.Legacy); var codeDocument = projectEngine.Process(projectItem); // Get the generated C# code var csharpDocument = codeDocument.GetRequiredCSharpDocument(); Console.WriteLine(csharpDocument.Text); // Check for compilation diagnostics foreach (var diagnostic in csharpDocument.Diagnostics) { Console.WriteLine($"{diagnostic.Severity}: {diagnostic.GetMessage()}"); } ``` -------------------------------- ### Razor Markup and Directives Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpErrorTest/ReportsErrorIfClassBlockUnterminatedAtEOF.cspans.txt Illustrates the basic structure of Razor files, showing the separation between markup and directive blocks. No specific setup is required beyond a Razor environment. ```razor @page @model IndexModel

Welcome

This is a Razor page.

``` -------------------------------- ### Razor Document Structure Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/HtmlAttributeTest/SymbolBoundAttributes2.stree.txt Illustrates the parsing of a Razor document, showing how markup and C# code are organized. This example highlights a C# code block containing an HTML element with an attribute. ```razor @{ } ``` -------------------------------- ### Razor Document Structure Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpRazorCommentsTest/RazorCommentInImplicitExpressionMethodCall.stree.txt Illustrates the parsing of a simple Razor syntax containing a C# expression and a Razor comment. ```razor @foo(/*@*/ ) ``` -------------------------------- ### Razor Document Structure Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpErrorTest/ShouldReportErrorAndTerminateAtMarkupIfParenInImplicitExpressionUnclosed.stree.txt This example shows a basic Razor document structure containing a C# expression within markup. It highlights the parsing of HTML tags and embedded C# code. ```razor @Foo(Bar(Baz) Biz Boz ``` -------------------------------- ### Razor Using Directive Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpBlockTest/ParsesNamespaceAliasWithSemicolonForUsingKeywordIfIsInValidFormat.stree.txt Demonstrates the syntax for an aliased using directive within a Razor file. Ensure correct C# syntax for namespaces and aliases. ```cshtml @using FooBarBaz = FooBarBaz; ``` -------------------------------- ### Razor Document Parsing Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpPreprocessorTest/DirectiveAfterCSharpCode_08.stree.txt Illustrates a simple Razor document containing an if statement within a C# code block and some HTML markup. This example shows the basic structure recognized by the Razor parser. ```razor @{LF if (true #if true)LF
Test
LF} ``` -------------------------------- ### Custom P Tag Helper Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.ir.txt Demonstrates the usage of a custom P Tag Helper. ```cshtml

New Time:

``` -------------------------------- ### Import RenderTreeBuilder Namespace Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Legacy_3_1_WhiteSpace_InMarkupInFunctionsBlock/TestComponent.mappings.txt This using directive is necessary for using the RenderTreeBuilder. ```csharp using Microsoft.AspNetCore.Components.Rendering ``` -------------------------------- ### Razor View Component Tag Helper Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ViewComponentTagHelper_Runtime.ir.txt Example demonstrating the use of a View Component Tag Helper within a Razor page, including C# variable declaration and HTML attribute binding. ```csharp var foo = "Hello"; ``` ```html ``` ```csharp BeginContext(59, 2, true); ``` ```html ``` ```csharp EndContext(); ``` ```csharp BeginContext(61, 50, false); ``` ```cshtml ``` ```csharp EndContext(); ``` -------------------------------- ### Razor Foreach Loop Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpErrorTest/TerminatesForeachBlockAtEOLWhenRecoveringFromMissingCloseParen.stree.txt This snippet demonstrates a basic C# foreach loop within Razor syntax, followed by literal markup. Ensure correct syntax for loops and delimiters. ```razor @foreach(foo bar baz ``` -------------------------------- ### Clean VS Code Test Artifacts Source: https://github.com/dotnet/razor/blob/main/src/Razor/test/Microsoft.VisualStudioCode.Razor.IntegrationTests/README.md Remove the entire .vscode-test directory to reset VS Code installations, extensions, and user data. This forces a fresh download and installation on the next test run. ```powershell # From repository root Remove-Item -Recurse -Force .\.vscode-test ``` -------------------------------- ### Custom PTagHelper Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicTagHelpers_DesignTime.ir.txt Illustrates a custom PTagHelper that can render HTML content and attributes. Use this for creating reusable components with specific behaviors. ```cshtml

``` -------------------------------- ### Razor C# Switch Statement Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CSharp7_Runtime.ir.txt Use a switch statement in Razor to conditionally render content based on the type or value of a variable. This example demonstrates handling different types, including a null case. ```cshtml switch (entry.Extra) { case int age: // Do something break; case IEnumerable childrenNames: // Do more something break; case null: // Do even more of something break; } ``` -------------------------------- ### Create RazorCodeDocument Source: https://context7.com/dotnet/razor/llms.txt Demonstrates creating a RazorCodeDocument from source, with parser and code generation options, and with imports. ```csharp using Microsoft.AspNetCore.Razor.Language; using System.Collections.Immutable; // Create a basic code document var source = RazorSourceDocument.Create(@"

Hello World

@DateTime.Now

", "Index.cshtml"); var codeDocument = RazorCodeDocument.Create(source); ``` ```csharp // Create with parser options for a specific file kind var parserOptions = new RazorParserOptions.Builder( RazorLanguageVersion.Latest, RazorFileKind.Component) { DesignTime = false }.ToOptions(); var codeGenerationOptions = new RazorCodeGenerationOptions.Builder() { RootNamespace = "MyApp.Components", SuppressChecksum = false }.ToOptions(); var componentDocument = RazorCodeDocument.Create( source, imports: ImmutableArray.Empty, parserOptions, codeGenerationOptions); ``` ```csharp // Create with imports (_ViewImports.cshtml) var importsContent = @" @using System.Collections.Generic @using MyApp.Models @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers "; var importsSource = RazorSourceDocument.Create(importsContent, "_ViewImports.cshtml"); var documentWithImports = RazorCodeDocument.Create( source, imports: [importsSource], parserOptions: null, codeGenerationOptions: null); ``` -------------------------------- ### Razor Tag Helper Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/CSharpPreprocessorTest/ElseNotOnNewline.cspans.txt Provides an example of a Tag Helper in Razor, which allows you to use HTML-like tags to render server-side logic. Tag helpers enhance the development experience by using familiar HTML syntax. ```cshtml ``` -------------------------------- ### Razor Markup Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpImplicitExpressionTest/ParsesNullForgivenessOperatorImplicitExpression_DirectiveCodeBlock.cspans.txt Basic Razor markup structure. ```cshtml

Welcome

``` -------------------------------- ### Razor Markup Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/ParsesExpressionOnSwitchCharacterFollowedByIdentifierStart.cspans.txt This snippet demonstrates basic HTML markup within a Razor file. It is rendered directly to the output. ```html

Hello, Razor!

``` -------------------------------- ### Configure RazorSourceGenerator via MSBuild Source: https://context7.com/dotnet/razor/llms.txt Provides an example of how to configure the RazorSourceGenerator in a .csproj file to enable compile-time compilation of Razor and CSHTML files. Includes settings for target framework, Razor language version, and output paths. ```xml net8.0 8.0 MyApp true true $(BaseIntermediateOutputPath)\GeneratedFiles ``` -------------------------------- ### Razor If Statement Start Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Instrumented_DesignTime.mappings.txt Begins an if statement in Razor syntax. ```cshtml |if(i == 11) { | ``` -------------------------------- ### Razor While Loop Start Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Instrumented_DesignTime.mappings.txt Initiates a while loop in Razor syntax. ```cshtml |while(i <= 10) { | ``` -------------------------------- ### Razor Document Structure Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/HtmlDocumentTest/OpenAngle_04.stree.txt Illustrates the parsing of a Razor document, showing the breakdown of markup and C# code elements. This is useful for understanding how Razor syntax is interpreted. ```text RazorDocument - [0..11)::11 - [< @("a")x@b] MarkupBlock - [0..11)::11 MarkupElement - [0..11)::11 MarkupStartTag - [0..11)::11 - [< @("a")x@b] - Gen OpenAngle;[<]; Text;[]; MarkupMiscAttributeContent - [1..8)::7 MarkupTextLiteral - [1..2)::1 - [ ] - Gen Whitespace;[ ]; CSharpCodeBlock - [2..8)::6 CSharpExplicitExpression - [2..8)::6 CSharpTransition - [2..3)::1 - Gen Transition;[@]; CSharpExplicitExpressionBody - [3..8)::5 RazorMetaCode - [3..4)::1 - Gen LeftParenthesis;[(]; CSharpCodeBlock - [4..7)::3 CSharpExpressionLiteral - [4..7)::3 - ["a"] - Gen StringLiteral;["a"]; RazorMetaCode - [7..8)::1 - Gen RightParenthesis;[)]; MarkupMinimizedAttributeBlock - [8..11)::3 - [x@b] MarkupTextLiteral - [8..11)::3 - [x@b] - Gen Text;[x@b]; CloseAngle;[]; EndOfFile;[]; ``` -------------------------------- ### Basic HTML Rendering in Razor Source: https://github.com/dotnet/razor/blob/main/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/TestFiles/SemanticTokens/RenderFragment.txt Illustrates how to render basic HTML elements within a Razor file. No special setup is required. ```html
``` -------------------------------- ### Razor Markup Block Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperBlockRewriterTest/UnderstandsMinimizedAttributes_Block31.cspans.txt Shows a Razor markup block. ```html Markup span at (25:0,25 [2] ) - Parent: Markup block at (12:0,12 [17] ) Markup span at (27:0,27 [2] ) - Parent: Markup block at (12:0,12 [17] ) Markup span at (46:0,46 [0] ) - Parent: Markup block at (0:0,0 [46] ) ``` -------------------------------- ### Razor Code Completion Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/HiddenSpansInCode_DesignTime.mappings.txt Demonstrates a simple case of code completion in Razor, showing how the '@' symbol initiates code parsing. ```cshtml @Da ``` -------------------------------- ### Basic HTML Tag Helper Usage Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SymbolBoundAttributes_DesignTime.ir.txt Demonstrates the use of the Html tag helper for binding to item properties. ```cshtml
``` -------------------------------- ### Razor Loop Syntax Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ProducesStandardLinePragmaForCSharpCode/TestComponent.mappings.txt Demonstrates a basic for loop in Razor syntax. ```razor for (var i = 0; i < 10; i++) { } ``` -------------------------------- ### Object Parameter Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/AddAttribute_ImplicitStringConversion_Bind/TestComponent.mappings.txt Represents an object parameter in a Razor component. ```cshtml ObjectParameter ``` -------------------------------- ### Razor Markup and Directives Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpSectionTest/HandlesUnterminatedSectionWithNestedIf.cspans.txt Illustrates basic Razor markup and directive usage. ```cshtml @{ // Code block for directives } ``` -------------------------------- ### Delegate Parameter Example Source: https://github.com/dotnet/razor/blob/main/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/AddAttribute_ImplicitStringConversion_Bind/TestComponent.mappings.txt Represents a delegate parameter in a Razor component. ```cshtml DelegateParameter ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.