{"id":21990,"date":"2014-08-21T12:31:50","date_gmt":"2014-08-21T19:31:50","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/aspnet\/?p=21990"},"modified":"2014-08-21T12:31:50","modified_gmt":"2014-08-21T19:31:50","slug":"command-line-scaffolding-for-asp-net-vnext","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/command-line-scaffolding-for-asp-net-vnext\/","title":{"rendered":"Command line scaffolding for ASP.NET vNext"},"content":{"rendered":"<p>In previous releases we have added support for scaffolding controllers\/views based on project artifacts in both MVC and Web Forms projects. The technology used for those previous releases was tied to Visual Studio. With ASP.NET vNext one of our high level goals is to enable Visual Studio customers to work with non-Visual Studio users. So the most critical aspects of developing\/testing ASP.NET vNext projects should be available to the non-Visual Studio. Scaffolding is a good example of this.<\/p>\n<p>Because we need to support scaffolding content into a project for both VS and non-VS scenarios we have gone back to the drawing board for scaffolding in ASP.NET vNext projects. In this post you I will describe how you can get started trying out the scaffolding features. Note: this is a very early preview for scaffolding, many features do not exist yet so please do not be too disappointed when you are trying these out. We are tying to get features into your hands as early as possible so that is one side effect of this.<\/p>\n<h2>How to get started with scaffolding<\/h2>\n<p>I\u2019ll show you how you can create a new project vNext web application in Visual Studio and then the modifications that are needed to get started with scaffolding. Currently the scaffolding does not work for empty projects, so I\u2019ll start with the ASP.NET vNext Web Application template. We will add support for the empty template in a future release.<\/p>\n<p>OK let\u2019s get you started with scaffolding. The first thing we are going to do is to create a new <strong>ASP.NET vNext Web Application<\/strong> project. You can see a screenshot of the selected template below.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/8712.image_45597778.png\"><img decoding=\"async\" title=\"image\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;float: none;margin-left: auto;border-top-width: 0px;margin-right: auto\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/1307.image_thumb_205BB6FF.png\" width=\"959\" height=\"664\" \/><\/a>\u00a0<\/p>\n<p>After creating this project we need to make a few one-time changes to enable downloading our pre-release NuGet packages. You can do this using the Package Manager Settings button in the Package Manager Console. If you don\u2019t see the Package Manager Console you can open it from View-&gt;Other Windows-&gt;Package Manager Console.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/3301.image_7753A8B3.png\"><img decoding=\"async\" title=\"image\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/8688.image_thumb_24689582.png\" width=\"825\" height=\"179\" \/><\/a><\/p>\n<p>You should configure you\u2019re new feed with the following values.<\/p>\n<ul>\n<li>Name = ASP.NET vNext <\/li>\n<li>Source = <a href=\"https:\/\/www.myget.org\/F\/aspnetvnext\/api\/v2\">https:\/\/www.myget.org\/F\/aspnetvnext\/api\/v2<\/a> <\/li>\n<\/ul>\n<p>You can see the screenshot below of what the result should look like. Don\u2019t forget to click Update after setting those values.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/1830.image_74AD7DB3.png\"><img decoding=\"async\" title=\"image\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/4382.image_thumb_6D8E413B.png\" width=\"761\" height=\"445\" \/><\/a>\u00a0<\/p>\n<p>Now that we have this configured we have access to the pre-release NuGet packages for ASP.NET vNext. To enable command line scaffolding there are a few edits to the project.json that you\u2019ll need to make. The required changes are summarized below.<\/p>\n<ol>\n<li>Add <strong><font size=\"2\" face=\"Consol\">&#8220;Microsoft.Framework.CodeGenerators.Mvc&#8221;: &#8220;1.0.0-*&#8221;<\/font><\/strong> to the dependencies section <\/li>\n<li>Add <font size=\"2\"><strong>&#8220;gen&#8221;: &#8220;Microsoft.Framework.CodeGeneration&#8221;<\/strong><\/font> to the commands section <\/li>\n<li>Update alpha3\/alpha2 to be * which will enable downloading the latest versions of each package <\/li>\n<\/ol>\n<p>The final project.json should look like the following.<\/p>\n<pre>{\n    \"dependencies\": {\n        \"EntityFramework.SqlServer\": \"7.0.0-*\",\n        \"Microsoft.AspNet.Mvc\": \"6.0.0-*\",\n        \"Microsoft.AspNet.Identity.EntityFramework\": \"3.0.0-*\",\n        \"Microsoft.AspNet.Identity.Authentication\": \"3.0.0-*\",\n        \"Microsoft.AspNet.Security.Cookies\": \"1.0.0-*\",\n        \"Microsoft.AspNet.Server.IIS\": \"1.0.0-*\",\n        \"Microsoft.AspNet.Server.WebListener\": \"1.0.0-*\",\n        \"Microsoft.AspNet.StaticFiles\": \"1.0.0-*\",\n        \"Microsoft.Framework.ConfigurationModel.Json\": \"1.0.0-*\",\n        \"Microsoft.VisualStudio.Web.BrowserLink.Loader\": \"14.0-*\",\n        \"Microsoft.Framework.CodeGenerators.Mvc\": \"1.0.0-*\"\n    },\n    \"commands\": {\n        \/* Change the port number when you are self hosting this application *\/\n        \"web\": \"Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http:\/\/localhost:5000\",\n        \"gen\": \"Microsoft.Framework.CodeGeneration\"\n    },\n    \"frameworks\": {\n        \"net451\" : { },\n        \"k10\" : { }\n    }\n}<\/pre>\n<p>Now that we have the project configured we will now go to the command line. There is some one-time setup there as well. I\u2019ll get you started with that now.<\/p>\n<p>From an **admin command prompt **execute the following command.<\/p>\n<p><font size=\"2\" face=\"Consolas\"><strong>powershell -NoProfile -ExecutionPolicy unrestricted -Command &#8220;iex ((new-object net.webclient).DownloadString(&#8216;https:\/\/raw.githubusercontent.com\/aspnet\/Home\/master\/kvminstall.ps1&#8217;))&#8221;<\/strong><\/font><\/p>\n<p>You should exit the command prompt after executing this command. Some environment variables are set that need to be applied before continuing.<\/p>\n<p>Now we need to download the latest kvm package. To do this <strong>open a new command prompt<\/strong> (<em>Note: for the next few steps do not use a PowerShell prompt<\/em>) execute the following two commands.<\/p>\n<p><font size=\"2\" face=\"Consolas\"><strong>set KRE_NUGET_API_URL=https:\/\/www.myget.org\/F\/aspnetvnext\/api\/v2<\/strong><\/font><\/p>\n<p><font size=\"2\" face=\"Consolas\"><strong>kvm upgrade<\/strong><\/font><\/p>\n<p>This will give us access to the latest version of kvm which is required for scaffolding. This will apply to the current session as well as future sessions for the command\/PowerShell prompt. <strong>After executing this you should restart any instances of Visual Studio that are currently open so that Visual Studio can pick up that setting.<\/strong><\/p>\n<p>Now let\u2019s get started with the actual scaffolding. For this sample I\u2019ll show you how you can add a new model to your project and then scaffold some new content based on that. Let\u2019s start by adding the Person class below to the project in the Models folder.<\/p>\n<pre>using System;\n\nnamespace WebApplication2\n{\n    public class Person\n    {\n        public int PersonId { get; set; }\n\n        public string Name { get; set; }\n\n        public bool BoolProperty { get; set; }\n    }\n}<\/pre>\n<p>Now that we\u2019ve added the new model, let\u2019s scaffold a controller and view to so that we can interact with it. In the command prompt you should navigate (cd) to the directory for the project (the directory that has the .kproj and project.json file). From there we will execute the following command.<\/p>\n<p><font size=\"2\" face=\"Consol\"><strong>k gen controller -m Person -dc PersonContext<\/strong><\/font><\/p>\n<p>Note: you could also use the fully qualified name for the model if you have more than one Person class. For example.<\/p>\n<p><font size=\"2\" face=\"Consol\"><strong>k gen controller -m WebApplication2.Person -dc WebApplication2.PersonContext<\/strong><\/font><\/p>\n<p>The parameters passed in indicate that the model (<font size=\"2\" face=\"Consolas\"><strong>-m<\/strong><\/font>) Person and the data context (<font size=\"2\" face=\"Consolas\"><strong>-dc<\/strong><\/font>) to be created is PersonContext. <em>(To learn more about the options you can execute <font size=\"2\" face=\"consol\"><strong>k gen controller &#8211;help<\/strong><\/font>.)<\/em><\/p>\n<p>The command executed will generate some artifacts into your project. During generation the command line will report added\/modified artifacts. In the screenshots below you can see the command line output. There is a screenshot of the new files in Solution Explorer as well.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/8524.image_6264B6F1.png\"><img decoding=\"async\" title=\"image\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/7446.image_thumb_4249AA34.png\" width=\"1037\" height=\"116\" \/><\/a><\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/5383.image_0D3D1B04.png\"><img decoding=\"async\" title=\"image\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;float: none;margin-left: auto;border-top-width: 0px;margin-right: auto\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/2262.image_thumb_56CEF9B2.png\" width=\"352\" height=\"584\" \/><\/a>\u00a0<\/p>\n<p>This is similar to the content that is generated for projects when using Visual Studio 2013. Now that you\u2019ve generated these artifacts you can navigate to <font size=\"2\" face=\"Consolas\"><strong>\/Person <\/strong><\/font>to see the Index view and start interacting with this model. At this point you may want to play around with the views\/controllers created in your project to get a sense for the updates. Now that we\u2019ve gone through the basics I will wrap up this post and include some details on what\u2019s next.<\/p>\n<h2>Summary<\/h2>\n<p>In this post I\u2019ve shown how you can get started with ASP.NET vNext command line scaffolding. Our current support is pretty limited, but we are working hard to extend this support and to bring this functionality into Visual Studio. Keep an eye on this blog as well as <a href=\"http:\/\/asp.net\/vNext\">http:\/\/asp.net\/vNext<\/a> for further updates. If you have any comments\/questions please let us know in the comments section below.<\/p>\n<h2>Known Issues<\/h2>\n<ul>\n<li>We do not support scaffolding into an ASP.NET vNext Empty Application, only the ASP.NET vNext Web Application template works today <\/li>\n<li>Formatting for scaffolded content is not ideal. After generating the content you can format the document using the Edit menu. In later releases we will format the generated content much better than we do today. <\/li>\n<li>From the command line if you execute kpm restore and get errors due to unable to locate packages you can execute the following modified command <strong><font size=\"2\" face=\"Consolas\">kpm restore \u2013s <\/font><\/strong><a href=\"https:\/\/www.myget.org\/F\/aspnetvnext\/api\/v2\"><strong><font size=\"2\" face=\"Consolas\">https:\/\/www.myget.org\/F\/aspnetvnext\/api\/v2<\/font><\/strong><\/a>.<\/li>\n<\/ul>\n<p>Sayed Ibrahim Hashimi | <a href=\"https:\/\/twitter.com\/sayedihashimi\">@SayedIHashimi<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In previous releases we have added support for scaffolding controllers\/views based on project artifacts in both MVC and Web Forms projects. The technology used for those previous releases was tied to Visual Studio. With ASP.NET vNext one of our high level goals is to enable Visual Studio customers to work with non-Visual Studio users. So [&hellip;]<\/p>\n","protected":false},"author":357,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[30,7265,7465,7461,7473],"class_list":["post-21990","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet","tag-announcement","tag-announcements","tag-asp-net-vnext","tag-scaffolding","tag-visual-studio-14"],"acf":[],"blog_post_summary":"<p>In previous releases we have added support for scaffolding controllers\/views based on project artifacts in both MVC and Web Forms projects. The technology used for those previous releases was tied to Visual Studio. With ASP.NET vNext one of our high level goals is to enable Visual Studio customers to work with non-Visual Studio users. So [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/21990","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\/357"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=21990"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/21990\/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=21990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=21990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=21990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}