{"id":5661,"date":"2015-04-24T06:20:00","date_gmt":"2015-04-24T06:20:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/2015\/04\/24\/making-it-better-asp-net-with-visual-basic-14\/"},"modified":"2015-04-24T06:20:00","modified_gmt":"2015-04-24T06:20:00","slug":"making-it-better-asp-net-with-visual-basic-14","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/making-it-better-asp-net-with-visual-basic-14\/","title":{"rendered":"Making It Better: ASP.NET with Visual Basic 14"},"content":{"rendered":"<p style=\"text-align: center\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/63\/56\/4578.SampleCode.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2015\/04\/4578.SampleCode.png\" alt=\"\" border=\"0\" \/><\/a>\n<\/p>\n<p>BASIC is part of the DNA of Microsoft, and we have continued to evolve the language for most of the 40 years of the company\u2019s existence.\u00a0 In the next evolution of the language, Visual Basic 14, there are a number of great features that are being added and we want to make sure that you know we are fully supporting them with ASP.NET in Visual Studio 2015.\u00a0 Let\u2019s take a look at a few samples of how using Visual Basic with ASP.NET 4.6 makes developers more productive and their code more readable.<\/p>\n<p>\u00a0<\/p>\n<h2>String Interpolation Makes Web Forms Strings Feel like Razor Syntax<\/h2>\n<p>String formatting with Visual Basic is usually a mess.\u00a0 How many times can you connect strings with ampersand (&amp;) marks and underscores when you span rows?\u00a0 If you assemble and format your strings by hand, you know what a mess this is.\u00a0 You already have some nice syntax in razor like the following:<\/p>\n<p>\u00a0<\/p>\n<pre class=\"scroll\"><code class=\"html\">&lt;a href=\"https:\/\/twitter.com\/@ViewBag.ScreenName\/status\/@Item.StatusID\"&gt;<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>If we wanted to do something similar in web forms for this anchor we could end up with a concatenated mess:<\/p>\n<p>\u00a0<\/p>\n<pre class=\"scroll\"><code class=\"html\">&lt;a href=\"https:\/\/twitter.com\/&lt;%: screenName %&gt;\/status\/&lt;%#: Item.StatusID %&gt;\"&gt;<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>Perhaps we got a little more creative and tried to use a String.Format method call:<\/p>\n<p>\u00a0<\/p>\n<pre class=\"scroll\"><code class=\"html\">&lt;a href=\"&lt;%#: String.Format(\"https:\/\/twitter.com\/{0}\/status\/{1}\", screenName, Item.StatusID) %&gt;\"&gt;<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>Yuck\u2026 That makes my eyes hurt to read that, with looking back and forth across the string to understand what values are formatted and placed into the output I feel like I\u2019m watching a tennis match. \u00a0We know we can do better to make this more readable. With the new string interpolation feature, the string can be decorated with a dollar symbol and then formatted with even simpler template text with a format like the following:<\/p>\n<p>\u00a0<\/p>\n<pre class=\"scroll\"><code class=\"cplusplus\">&lt;a href=\"&lt;%#: $\"https:\/\/twitter.com\/{screenName}\/status\/{Item.StatusID}\" %&gt;\"&gt;<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>Now our string looks more readable, just like the razor syntax without having to scan back and forth across the code.\u00a0 Values inside of curly braces in our text are interpreted as code values to be inserted at that point in the string, just like our previous <span style=\"font-family: 'courier new', courier\">String.Format<\/span> text format but without the annoying back and forth reading for us humans.\u00a0 This feature isn\u2019t limited to just ASP.NET use, as you can use it in all of the .NET frameworks that support Visual Basic 14.<\/p>\n<p>\u00a0<\/p>\n<h2>Use the New NameOf Operator in MVC to Validate Arguments<\/h2>\n<p>Magic strings are the practice of having string constants in our code that control behavior of our application.\u00a0 An example of this is when we throw the <span style=\"font-family: 'courier new', courier\">ArgumentNullException<\/span> when verifying input parameters:<\/p>\n<p>\u00a0<\/p>\n<pre class=\"scroll\"><code class=\"csharp\"> Public Function TwitterStatus(screenName As String) As ActionResult&lt;br \/&gt; &lt;br \/&gt; If (String.IsNullOrEmpty(screenName)) Then&lt;br \/&gt; &lt;br \/&gt; Throw New ArgumentNullException(\"screenName\")&lt;br \/&gt; &lt;br \/&gt; End If<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>That sample has a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Code_smell\">code smell<\/a>, or a deep-seeded problem\u2026 if the parameter name changes and an error scenario is triggered, our code will break with this ugliness:<\/p>\n<p style=\"text-align: center\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/63\/56\/0027.mvcError.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2015\/04\/0027.mvcError.png\" alt=\"\" border=\"0\" \/><\/a>\u00a0\n<\/p>\n<p>As someone who reads stack traces and debugger output, this helpful bit of information that the previous developer attempted to provide for me no longer makes any sense.<\/p>\n<p>The new <span style=\"font-family: 'courier new', courier\">NameOf<\/span> operator will take a variable and return a string value for the name of that variable.\u00a0 Take a look at how our code changes when we use the <span style=\"font-family: 'courier new', courier\">NameOf<\/span> operator:<\/p>\n<p>\u00a0<\/p>\n<pre class=\"scroll\"><code class=\"csharp\"> Public Function TwitterStatus(screenName As String) As ActionResult&lt;br \/&gt; &lt;br \/&gt; If (String.IsNullOrEmpty(screenName)) Then&lt;br \/&gt; &lt;br \/&gt; Throw New ArgumentNullException(NameOf(screenName))&lt;br \/&gt; &lt;br \/&gt; End If&lt;br \/&gt; <\/code><\/pre>\n<p>\u00a0<\/p>\n<p>With that change, we can protect our MVC and WebAPI action methods from inadvertent changes.\u00a0 Remember, the new refactoring capabilities in Visual Studio (or another refactoring tool that you may be using) will not capture references to the parameter in strings.\u00a0\u00a0 If you do use the automatic refactor it will not replace the parameter name in the previous string example, however it will capture this reference to the parameter name. Additionally, our thrown error looks like the following:<\/p>\n<p style=\"text-align: center\">\n  \u00a0<a href=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/63\/56\/2275.mvcError_better.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2015\/04\/2275.mvcError_better.png\" alt=\"\" border=\"0\" \/><\/a>\n<\/p>\n<p>Now that error statement makes more sense for the next developer who will be maintaining your code.<\/p>\n<p>\u00a0<\/p>\n<h2>Asynchronous Model Binding in Web Forms<\/h2>\n<p>Asynchronous operations have been available with the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/dd460717(v=vs.110).aspx\" target=\"_blank\" rel=\"noopener\">Task Parallel Library<\/a> since .NET 4 in 2011.\u00a0 It\u2019s been around for a long time, and Web Forms just has not had a chance to really feel that love in the more complex interactions that they host. Let\u2019s face it, Web Forms is the event-driven model that could have significant problems if an event is not finished an asynchronous operation when the next event is about to start.\u00a0 It\u2019s a complex model, but in the latest update to ASP.NET 4.6, we have opened the web forms model to allow Asynchronous Model Binding operations with the Task Parallel library.\u00a0 Let\u2019s take a look at how easy it is to migrate an existing set of controls to model binds asynchronously.<\/p>\n<p>In this sample, I\u2019m going to load the tweets for both the <a href=\"https:\/\/twitter.com\/visualstudio\">Visual Studio<\/a> and <a href=\"https:\/\/twitter.com\/aspnet\">ASP.NET<\/a> twitter accounts into two columns on a web page.\u00a0 I\u2019ll use the ASP.NET ListView control to output a simple list of the 5 most recent tweets with a syntax and formatting like this:<\/p>\n<p>\u00a0<\/p>\n<pre class=\"scroll\"><code class=\"html\">&lt;asp:ListView runat=\"server\" ID=\"vsTweetList\"&lt;br \/&gt; &lt;br \/&gt; SelectMethod=\"vsTweetList_GetData\" ItemType=\"LinqToTwitter.Status\"&gt;&lt;br \/&gt; &lt;br \/&gt; &lt;ItemTemplate&gt;&lt;br \/&gt; &lt;br \/&gt; &lt;p&gt;&lt;br \/&gt; &lt;br \/&gt; &lt;%#: $\"At: {Item.CreatedAt.ToString(\"T\")} on {Item.CreatedAt.ToString(\"d\")}\" %&gt;&lt;br \/&gt; &lt;br \/&gt; &lt;br \/&gt;&lt;br \/&gt; &lt;br \/&gt; &lt;%#: Item.Text %&gt;&lt;br \/&gt; &lt;br \/&gt; &lt;\/p&gt;&lt;br \/&gt; &lt;br \/&gt; &lt;\/ItemTemplate&gt;&lt;br \/&gt; &lt;br \/&gt; &lt;ItemSeparatorTemplate&gt;&lt;hr \/&gt;&lt;\/ItemSeparatorTemplate&gt;&lt;br \/&gt; &lt;br \/&gt; &lt;\/asp:ListView&gt;<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>I\u2019m using Model Binding with this control by declaring a <span style=\"font-family: 'courier new', courier\">SelectMethod<\/span> on the control.\u00a0 This method will be called when the control is ready to bind data for presentation, typically after the <span style=\"font-family: 'courier new', courier\">Page_Load<\/span> event.\u00a0 Model Binding for Web Forms is available starting with ASP.NET 4.5, and the syntax is not changed for 4.6\u00a0<\/p>\n<p>The <span style=\"font-family: 'courier new', courier\">ItemType<\/span> argument on the ListView provides strongly-typed databinding capabilities.\u00a0 This allows me to avoid the magic-string problem with databinding syntax like <span style=\"font-family: 'courier new', courier\">Eval(\u201cStatus\u201d)<\/span> If Status was misspelled or did not appear in my data object, I wouldn\u2019t see an error until the page was requested.\u00a0 By using the <span style=\"font-family: 'courier new', courier\">ItemType<\/span>, all of the references to Item in this control are strongly-typed and I get editor intellisense assistance.<\/p>\n<p>Notice I used the new string interpolation feature from Visual Basic in the date format.\u00a0 While not entirely necessary, it just felt cool to be able to write that as a complete sentence.<\/p>\n<p>The code-behind to fetch the tweets for this presentation would normally look something like this:<\/p>\n<p>\u00a0<\/p>\n<pre class=\"scroll\"><code class=\"csharp\"> Public Function vsTweetList_GetData() As IEnumerable(Of Status)&lt;br \/&gt; &lt;br \/&gt; Return GetTweetsFor(\"VisualStudio\")&lt;br \/&gt; &lt;br \/&gt; End Function<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>That would normally work really well, fetching the data and loading it into my resultant HTML very quickly.\u00a0 In this case, I want to load two sets of tweets.\u00a0 That means two requests to Twitter to load data and blocking page processing while those two requests are fulfilled by the Twitter APIs.<\/p>\n<p>We can make these requests asynchronous by changing the syntax of the specified <span style=\"font-family: 'courier new', courier\">SelectMethod<\/span> to return a Task.\u00a0 We also need to mark the <span style=\"font-family: 'courier new', courier\">@Page<\/span> directive with an <span style=\"font-family: 'courier new', courier\">async=\u201dtrue\u201d<\/span> attribute.\u00a0 It seems too simple, but the web forms framework was updated to allow this type of simple update to \u201cjust let the magic happen\u201d<\/p>\n<p>\u00a0<\/p>\n<pre class=\"scroll\"><code class=\"csharp\"> Public Async Function vsTweetList_GetDataAsync() _&lt;br \/&gt; &lt;br \/&gt; As Threading.Tasks.Task(Of IEnumerable(Of Status))&lt;br \/&gt; &lt;br \/&gt; Return Await GetTweetsForAsync(\"VisualStudio\")&lt;br \/&gt; &lt;br \/&gt; End Function&lt;br \/&gt; <\/code><\/pre>\n<p>\u00a0<\/p>\n<p>Seriously, that\u2019s all that you need to do in order to allow your requests to fire asynchronously.\u00a0 Just add the async keyword, return a task, and await the asynchronous method call.\u00a0 Now, both of my requests to the Twitter API will fire at the same time, the ASP.NET runtime will manage the threads that call the API and ensure that they are all processed before returning the page to the requesting user.<\/p>\n<p>\u00a0<\/p>\n<h2>Roslyn Support for Visual Basic Compilation<\/h2>\n<p><a style=\"float: right\" href=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/63\/56\/2275.vbCompiler.png\"><img decoding=\"async\" style=\"margin: 3px\" src=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2015\/04\/2275.vbCompiler.png\" alt=\"Graph of comparison between compiler versions\" width=\"343\" height=\"203\" \/><\/a>With the introduction of the new Roslyn compiler, \u00a0you now have a turbo-charged ASP.NET compiler experience.\u00a0 In the tests in our lab, we have measured an almost 50% speed increase for Visual Basic developers at compile time.\u00a0 However, we didn\u2019t stop there.<\/p>\n<p>Since May 2014, there has been a NuGet package available called \u201c<a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.CodeDom.Providers.DotNetCompilerPlatform\">CodeDOM Providers for .NET Compiler Platform (Roslyn)<\/a>\u201d and wow was that hard to find.\u00a0 Only 600 downloads of this package have been recorded at the time of this article\u2019s writing, and that\u2019s a real shame.\u00a0 This package activates the Roslyn compiler for ASPX page parsing and compiling.\u00a0 Do you remember waiting for the compiler to re-interpret your ASPX pages each time you change HTML formatting?\u00a0 This package cuts that time significantly, but is in pre-release mode and only works for C#-based ASPX files.\u00a0 With the coming release of Visual Studio 2015, we are going to release a 1.0 version of this package and add Visual Basic support.<\/p>\n<p>\u00a0<\/p>\n<h2>ASP.NET 5 \u2013 C# Support And Also Visual Basic<\/h2>\n<p>We\u2019ve talked about ASP.NET 5 as a major update of the ASP.NET framework with Roslyn and cross-platform support in mind since our initial public discussions.\u00a0 It is not a short path, and we focused initially on completing support for C#.\u00a0 In the months since our initial announcements, we have heard from many of you, telling us how much you like Visual Basic and that they want to see support for it in ASP.NET 5.\u00a0<\/p>\n<p>We are excited today to announce that ASP.NET 5 will have full support with Visual Basic (both tooling and runtime \u2013 including cross platform runtime support).\u00a0 As always, we will continue this development of ASP.NET 5 in the open, and you can track our progress or even contribute on GitHub at <a href=\"http:\/\/github.com\/aspnet\/home\">http:\/\/github.com\/aspnet\/home<\/a>.<\/p>\n<p>\u00a0<\/p>\n<h2>Summary<\/h2>\n<p>Visual Basic, ASP.NET and even Classic ASP before that have had a long history together.\u00a0 We\u2019re committed to that partnership, and we will continue to evolve the Visual Basic story with ASP.NET.<\/p>\n<p>You can learn more about the new features in Visual Basic 14 from <a href=\"https:\/\/channel9.msdn.com\/Events\/Visual-Studio\/Connect-event-2014\/113\">Lucian Wischik<\/a> and from <a href=\"https:\/\/www.wintellectnow.com\/Videos\/Watch?videoId=visual-basic-14\">Kathleen Dollard<\/a> in a pair of snack-sized five-minute videos.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>BASIC is part of the DNA of Microsoft, and we have continued to evolve the language for most of the 40 years of the company\u2019s existence.\u00a0 In the next evolution of the language, Visual Basic 14, there are a number of great features that are being added and we want to make sure that you [&hellip;]<\/p>\n","protected":false},"author":405,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[30,7265,7489,145,7491],"class_list":["post-5661","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet","tag-announcement","tag-announcements","tag-asp-net-5","tag-visual-basic","tag-visual-studio-2015"],"acf":[],"blog_post_summary":"<p>BASIC is part of the DNA of Microsoft, and we have continued to evolve the language for most of the 40 years of the company\u2019s existence.\u00a0 In the next evolution of the language, Visual Basic 14, there are a number of great features that are being added and we want to make sure that you [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/5661","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\/405"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=5661"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/5661\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=5661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=5661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=5661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}