{"id":55550,"date":"2025-02-13T10:05:00","date_gmt":"2025-02-13T18:05:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet\/?p=55550"},"modified":"2025-02-18T15:56:14","modified_gmt":"2025-02-18T23:56:14","slug":"enhancing-razor-productivity-with-new-features","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/enhancing-razor-productivity-with-new-features\/","title":{"rendered":"New Features for Enhanced Razor Productivity!"},"content":{"rendered":"<p>If you are building web apps with Razor, we have some great new features that you are going to love for both Visual Studio and Visual Studio Code! Extract to Component refactoring and the new Roslyn-based C# tokenizer are now available and are designed to improve your productivity in Razor files, let&#8217;s take a look.<\/p>\n<h2>Extract to Component<\/h2>\n<p>Extract to Component, available in Visual Studio 17.12, is a new refactoring that automates the process of creating a new Razor\/Blazor component. Instead of manually creating a new file and copy\/pasting the code you want to extract, selecting this feature will do that work for you by selecting the lightbulb refactoring (<code>CTRL + .<\/code>) after highlighting the code (or tag) you want to extract. This feature makes it easier to create reusable components, allowing for a cleaner and more manageable codebase.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/02\/Extract-To-Component-Weather.gif\" alt=\"Figure 1-Extract to Component example\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/02\/Extract-To-Component-Weather2.gif\" alt=\"Figure 2- Highlighting table tag to extract table to new component\" \/><\/p>\n<p>In this first iteration of the feature, Extract to Component focuses on support for basic, mostly HTML-based extraction scenarios. However, we have plans to add additional improvements and more advanced scenarios (i.e. more consistent extractions involving variable dependencies, C#, parameters, etc.).<\/p>\n<h2>Roslyn C# Tokenizer<\/h2>\n<p>The C# tokenizer \/ lexer update brings significant improvements to how Razor handles C# code. Many users have expressed frustrations with not being able to use raw string literals and verbatim interpolated strings in Razor files, and the new Roslyn C# lexer fixes that! In addition to these string formats, the lexer also adds support for binary literals and improves the handling of C# preprocessor directives, ensuring they follow C# rules. Ultimately, the new lexer will also make it easier to support new C# language features going forward.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/02\/roslyn-\" alt=\"Figure 3- Examples of strings now supported in Razor files by Roslyn tokenizer\" \/><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/02\/roslyn-tokenizer-string-output.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-55560\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/02\/roslyn-tokenizer-string-output.png\" alt=\"Image roslyn tokenizer string output\" width=\"814\" height=\"250\" srcset=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/02\/roslyn-tokenizer-string-output.png 814w, https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/02\/roslyn-tokenizer-string-output-300x92.png 300w, https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/02\/roslyn-tokenizer-string-output-768x236.png 768w\" sizes=\"(max-width: 814px) 100vw, 814px\" \/><\/a><\/p>\n<p>This new tokenizer is not on by default until .NET 10 but is available in both Visual Studio (17.13) and Visual Studio Code for .NET 9. To enable the C# tokenizer today, check the <strong>Use the C# tokenizer for Razor files in the IDE<\/strong> option under <strong>Tools &gt; Options &gt; Preview Features<\/strong> and add <code>&lt;Features&gt;use-roslyn-tokenizer;$(Features)&lt;\/Features&gt;<\/code> to a property group in your <code>.csproj<\/code> or <code>directory.props<\/code> file:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/02\/roslyn-tokenizer-csproj-line.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-55595\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/02\/roslyn-tokenizer-csproj-line.png\" alt=\"Image roslyn tokenizer csproj line\" width=\"932\" height=\"369\" srcset=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/02\/roslyn-tokenizer-csproj-line.png 932w, https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/02\/roslyn-tokenizer-csproj-line-300x119.png 300w, https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/02\/roslyn-tokenizer-csproj-line-768x304.png 768w\" sizes=\"(max-width: 932px) 100vw, 932px\" \/><\/a><\/p>\n<p>This new lexer does currently come with some breaking changes, particularly around preprocessor directives, so we encourage you to please share any related issues you may experience in the <a href=\"https:\/\/github.com\/dotnet\/razor\">Razor Github repository<\/a>.<\/p>\n<h2>Summary<\/h2>\n<p>These two updates, Extract to Component and the C# tokenizer, help enhance your Razor productivity. By adopting these features, you can ensure cleaner code, better language support, and an overall more efficient development process. However, there\u2019s always room for improvement! To share your Razor feedback, submit issues in our <a href=\"https:\/\/github.com\/dotnet\/razor\">Razor Github repo<\/a>, the <a href=\"https:\/\/developercommunity.visualstudio.com\/\">Developer Community<\/a>, or <a href=\"https:\/\/www.research.net\/r\/9RP9SVP\">check out this survey<\/a> to share your Extract to Component feedback!<\/p>\n<p>Finally, if you\u2019d like to chat directly with the Razor team about our upcoming roadmap and how we\u2019re addressing your issues, you can join our upcoming <a href=\"https:\/\/dotnet.microsoft.com\/live\/community-standup\">.NET Community Standup<\/a> on February 18th!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Extract to Component refactoring and the Roslyn tokenizer are two new features designed to help improve your productivity in Razor files.<\/p>\n","protected":false},"author":651,"featured_media":55551,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685,7509,7251,646,7593],"tags":[7205,7384],"class_list":["post-55550","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","category-aspnetcore","category-blazor","category-visual-studio","category-visual-studio-code","tag-blazor","tag-razor"],"acf":[],"blog_post_summary":"<p>The Extract to Component refactoring and the Roslyn tokenizer are two new features designed to help improve your productivity in Razor files.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/55550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/651"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=55550"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/55550\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/55551"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=55550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=55550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=55550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}