{"id":228087,"date":"2020-01-22T08:53:20","date_gmt":"2020-01-22T16:53:20","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/visualstudio\/?p=228087"},"modified":"2020-11-04T14:42:14","modified_gmt":"2020-11-04T22:42:14","slug":"getting-started-with-blazor-server-apps-in-visual-studio-for-mac","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/getting-started-with-blazor-server-apps-in-visual-studio-for-mac\/","title":{"rendered":"Getting Started with Blazor Server Apps in Visual Studio for Mac"},"content":{"rendered":"<p>In Visual Studio 2019 for Mac <a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/visual-studio-2019-for-mac-version-8-4-is-now-available\/\">v8.4<\/a> one of the big things that we added support for is developing <a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/blazor\/?view=aspnetcore-3.1\">Blazor Server Applications<\/a>. In this post I\u2019ll show you how you can get started building new Blazor Server applications with Visual Studio for Mac. Blazor lets you build interactive web UIs using C# instead of JavaScript. Blazor apps are composed of reusable web UI components implemented using C#, HTML, and CSS. Both client and server code are written in C#, allowing you to share code and libraries.<\/p>\n<h2>Creating a new Blazor Server Project<\/h2>\n<p>When you first launch Visual Studio for Mac you will see the dialog that follows:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-228088 size-large\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/get-to-code-1024x627.png\" alt=\"\" width=\"640\" height=\"392\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/get-to-code-1024x627.png 1024w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/get-to-code-300x184.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/get-to-code-768x470.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/get-to-code.png 1440w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>To get started you will first click New to begin creating your new Blazor Server app. You can also use the menu option <em>File-&gt;New Solution<\/em> as shown below.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-228090 size-large\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-new-solution-1-1024x545.png\" alt=\"\" width=\"640\" height=\"341\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-new-solution-1-1024x545.png 1024w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-new-solution-1-300x160.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-new-solution-1-768x409.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-new-solution-1.png 1044w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>Once you\u2019ve done that, the New Project Dialog will appear. To create a Blazor Server app we will go to the <em>.NET Core -&gt; App<\/em> section and then select <em>Blazor Server App<\/em>:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-228091 size-large\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/new-project-1024x743.png\" alt=\"\" width=\"640\" height=\"464\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/new-project-1024x743.png 1024w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/new-project-300x218.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/new-project-768x557.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/new-project-1536x1115.png 1536w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/new-project.png 1802w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>After clicking Next, you\u2019ll be prompted to select the .NET Core version. You can select the default value, .NET Core 3.1 at the time of this post, or change it to use a specific version. For Blazor apps, .NET Core 3.0 or newer is required. Once you\u2019ve selected Next, you\u2019ll get to the next page in the wizard where you will give your new project a name. I have named this new project HelloBlazor.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-large wp-image-228092\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/new-project-3-1024x743.png\" alt=\"\" width=\"640\" height=\"464\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/new-project-3-1024x743.png 1024w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/new-project-3-300x218.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/new-project-3-768x557.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/new-project-3-1536x1115.png 1536w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/new-project-3.png 1802w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>Now that we have configured our new project we can click Create (or hit the Return key) to create the project. After the project is created, it will be opened in the IDE. I have opened the Index.razor file in Visual Studio for Mac\u2019s editor, which you can see in the screenshot below.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-228093\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-with-blazor-project-01-1024x541.png\" alt=\"\" width=\"1095\" height=\"578\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-with-blazor-project-01-1024x541.png 1024w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-with-blazor-project-01-300x158.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-with-blazor-project-01-768x405.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-with-blazor-project-01-1536x811.png 1536w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-with-blazor-project-01-2048x1081.png 2048w\" sizes=\"(max-width: 1095px) 100vw, 1095px\" \/><\/p>\n<p>Now that the project has been created, the first thing that we should do is to run the application to ensure that everything is working as expected. You can start your new Blazor app with <em>Run &gt; Start Debugging<\/em><em> or Run &gt; Start without Debugging<\/em>.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-228095 size-medium\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-run-menu-start01-1-300x182.png\" alt=\"\" width=\"300\" height=\"182\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-run-menu-start01-1-300x182.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-run-menu-start01-1.png 638w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>In this case, let\u2019s go with Start without Debugging because it launches faster than a debug session, and we are not intending to do any debugging currently. To Start without Debugging you can use the menu option (shown in image above), or you can use the keyboard shortcut \u2325\u2318\u23ce. When you start your application, it will be launched within the default system browser., You can change the launched browser by using the browser selector in the toolbar, shown in next image.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-228096 size-full\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-browser-selector2.png\" alt=\"\" width=\"820\" height=\"214\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-browser-selector2.png 820w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-browser-selector2-300x78.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-browser-selector2-768x200.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/p>\n<p>Let\u2019s start this app with the <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/mac\/keyboard-shortcuts?view=vsmac-2019\">keyboard shortcut<\/a> for Start without Debugging. After the project is built, the app will be opened in a browser. Now that we have our project up and running, let\u2019s have some fun and customize it a bit.<\/p>\n<p>In the project that is created there is a Counter page where you can click a button to increment the count. Let\u2019s modify this to page to enable the user to specify the increment value. We can do this by adding an input field onto the Counter page and binding it to a new increment property that is used to increment the counter. Take a look at the updated code for Counter.razor in the following screenshot.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-228097 size-full\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-blazor-counter.png\" alt=\"\" width=\"2190\" height=\"1156\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-blazor-counter.png 2190w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-blazor-counter-300x158.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-blazor-counter-1024x541.png 1024w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-blazor-counter-768x405.png 768w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-blazor-counter-1536x811.png 1536w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-blazor-counter-2048x1081.png 2048w\" sizes=\"(max-width: 2190px) 100vw, 2190px\" \/><\/p>\n<p>If you would like to copy and paste the code into your project, a snippet is below.<\/p>\n<div>\n<pre class=\"code \">@page \"\/counter\"\r\n\r\n&lt;h1&gt;Counter&lt;\/h1&gt;\r\n\r\n&lt;input type=\"number\" min=\"1\" step=\"1\" @bind-value=\"increment\" \/&gt;\r\n&lt;p&gt;Current count: @currentCount&lt;\/p&gt;\r\n\r\n&lt;button class=\"btn btn-primary\" @onclick=\"IncrementCount\"&gt;Click me&lt;\/button&gt;\r\n\r\n@code {\r\n    public int increment = 1;\r\n    private int currentCount = 0;\r\n\r\n    private void IncrementCount()\r\n    {\r\n        currentCount += increment;\r\n    }\r\n}<\/pre>\n<\/div>\n<p>In the code shown above the lines indicated by an arrow are the new, or edited, lines of code. Here we have added a new input field (line 5) for users to configure the increment, we have added a new property increment (line 11) to store the increment value and lastly we have modified line 16 to use the increment value instead of the hard coded increment by 1.<\/p>\n<p>To ensure that the changes we have made are working as expected, we will start a debugging session. Let\u2019s set a breakpoint when <em>currentCount<\/em> is incremented, line 16. After setting that breakpoint, we will Start Debugging with the keyboard shortcut \u2318\u23ce. When the breakpoint is hit, we can verify that the value for increment is taken from the input field on the Counter page. The animated gif below shows; creating a breakpoint, debugging the application and inspecting the value for increment when the breakpoint is hit.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-228099\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/01\/vsmac-blazor-debugging.gif\" alt=\"\" width=\"1920\" height=\"1080\" \/><\/p>\n<p>If all goes well, the increment value was taken from the input field in the Counter page, and the app is behaving correctly. Now that we\u2019ve shown how you can create, edit and debug a Blazor Server app, it\u2019s time to wrap up this post.<\/p>\n<h2>Recap and next steps<\/h2>\n<p>In this post we have shown how to create a new Blazor Server application and work with it in Visual Studio for Mac. If you haven\u2019t already, <a href=\"https:\/\/visualstudio.microsoft.com\/vs\/mac\/\">download Visual Studio for Mac<\/a> to get started. If you are an existing Visual Studio for Mac user, <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/mac\/update?view=vsmac-2019\">update Visual Studio for Mac<\/a> to version 8.4 or newer to get support for Blazor Server apps. In addition to developing Blazor Server apps, you can also <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/mac\/blazor?view=vsmac-2019#publishing-blazor-applications-to-azure-app-service\">publish them to Azure App Services<\/a>.<\/p>\n<p>If you have any issues while working in Visual Studio for Mac, please <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/mac\/report-a-problem?view=vsmac-2019\">Report a Problem<\/a> so that we can improve the product. Before we go, here are some additional resources for you.<\/p>\n<h3>Additional Resources<\/h3>\n<p>To learn more about the changes in Visual Studio 2019 for Mac v8.4, take a look at the <a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/visual-studio-2019-for-mac-version-8-4-is-now-available\/\">v8.4 release blog post<\/a>.<\/p>\n<p>Join us for our upcoming <a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/announcing-visual-studio-for-mac-refresh-event-on-february-24\/\">Visual Studio for Mac: Refresh() event on February 24<\/a> for deep dive sessions into .NET development using Visual Studio for Mac, including a full session on developing Blazor applications.<\/p>\n<p>For more info on Blazor a good starting point is <a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/blazor\/?view=aspnetcore-3.1\">Introduction to ASP.NET Core Blazor<\/a>.<\/p>\n<p>For another guide on creating a Blazor Server application in Visual Studio for Mac head over to the docs at <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/mac\/blazor?view=vsmac-2019\">Create Blazor web apps<\/a>.<\/p>\n<p>Make sure to follow us on Twitter at <a href=\"https:\/\/twitter.com\/VisualStudioMac\">@VisualStudioMac<\/a> and reach out to the team. Customer feedback is important to us and we would love to hear your thoughts. Alternatively, you can head over to <a href=\"https:\/\/aka.ms\/feedback\/vsm-home\">Visual Studio Developer Community<\/a> to track your issues, <a href=\"https:\/\/aka.ms\/vsmac-suggestion\">suggest a feature<\/a>, ask questions, and find answers from others. We use your feedback to continue to improve Visual Studio 2019 for Mac, so thank you again on behalf of our entire team.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Visual Studio 2019 for Mac v8.4 one of the big things that we added support for is developing Blazor Server Applications. In this post I\u2019ll show you how you can get started building new Blazor Server applications with Visual Studio for Mac. Blazor lets you build interactive web UIs using C# instead of JavaScript. [&hellip;]<\/p>\n","protected":false},"author":357,"featured_media":226537,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[155,1029],"tags":[3743,452,4976],"class_list":["post-228087","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-studio","category-web","tag-visual-studio-2019-for-mac","tag-visual-studio-for-mac","tag-vsmac"],"acf":[],"blog_post_summary":"<p>In Visual Studio 2019 for Mac v8.4 one of the big things that we added support for is developing Blazor Server Applications. In this post I\u2019ll show you how you can get started building new Blazor Server applications with Visual Studio for Mac. Blazor lets you build interactive web UIs using C# instead of JavaScript. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/228087","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/users\/357"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/comments?post=228087"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/228087\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media\/226537"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media?parent=228087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=228087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=228087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}