Command line scaffolding for ASP.NET vNext

Command line scaffolding for ASP.NET vNext

  • Comments 4

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.

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.

How to get started with scaffolding

I’ll 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’ll start with the ASP.NET vNext Web Application template. We will add support for the empty template in a future release.

OK let’s get you started with scaffolding. The first thing we are going to do is to create a new ASP.NET vNext Web Application project. You can see a screenshot of the selected template below.

image 

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’t see the Package Manager Console you can open it from View->Other Windows->Package Manager Console.

image

You should configure you’re new feed with the following values.

You can see the screenshot below of what the result should look like. Don’t forget to click Update after setting those values.

image 

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’ll need to make. The required changes are summarized below.

  1. Add "Microsoft.Framework.CodeGenerators.Mvc": "1.0.0-*" to the dependencies section
  2. Add "gen": "Microsoft.Framework.CodeGeneration" to the commands section
  3. Update alpha3/alpha2 to be * which will enable downloading the latest versions of each package

The final project.json should look like the following.

{
    "dependencies": {
        "EntityFramework.SqlServer": "7.0.0-*",
        "Microsoft.AspNet.Mvc": "6.0.0-*",
        "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-*",
        "Microsoft.AspNet.Identity.Authentication": "3.0.0-*",
        "Microsoft.AspNet.Security.Cookies": "1.0.0-*",
        "Microsoft.AspNet.Server.IIS": "1.0.0-*",
        "Microsoft.AspNet.Server.WebListener": "1.0.0-*",
        "Microsoft.AspNet.StaticFiles": "1.0.0-*",
        "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*",
        "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0-*",
        "Microsoft.Framework.CodeGenerators.Mvc": "1.0.0-*"
    },
    "commands": {
        /* Change the port number when you are self hosting this application */
        "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000",
        "gen": "Microsoft.Framework.CodeGeneration"
    },
    "frameworks": {
        "net451" : { },
        "k10" : { }
    }
}

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’ll get you started with that now.

From an admin command prompt execute the following command.

powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.ps1'))"

You should exit the command prompt after executing this command. Some environment variables are set that need to be applied before continuing.

Now we need to download the latest kvm package. To do this open a new command prompt (Note: for the next few steps do not use a PowerShell prompt) execute the following two commands.

set KRE_NUGET_API_URL=https://www.myget.org/F/aspnetvnext/api/v2

kvm upgrade

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. After executing this you should restart any instances of Visual Studio that are currently open so that Visual Studio can pick up that setting.

Now let’s get started with the actual scaffolding. For this sample I’ll show you how you can add a new model to your project and then scaffold some new content based on that. Let’s start by adding the Person class below to the project in the Models folder.

using System;

namespace WebApplication2
{
    public class Person
    {
        public int PersonId { get; set; }

        public string Name { get; set; }

        public bool BoolProperty { get; set; }
    }
}

Now that we’ve added the new model, let’s 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.

k gen controller -m Person -dc PersonContext

Note: you could also use the fully qualified name for the model if you have more than one Person class. For example.

k gen controller -m WebApplication2.Person -dc WebApplication2.PersonContext

The parameters passed in indicate that the model (-m) Person and the data context (-dc) to be created is PersonContext. (To learn more about the options you can execute k gen controller --help.)

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.

image

image 

This is similar to the content that is generated for projects when using Visual Studio 2013. Now that you’ve generated these artifacts you can navigate to /Person 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’ve gone through the basics I will wrap up this post and include some details on what’s next.

Summary

In this post I’ve 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 http://asp.net/vNext for further updates. If you have any comments/questions please let us know in the comments section below.

Known Issues

  • We do not support scaffolding into an ASP.NET vNext Empty Application, only the ASP.NET vNext Web Application template works today
  • 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.
  • 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 kpm restore –s https://www.myget.org/F/aspnetvnext/api/v2.

Sayed Ibrahim Hashimi | @SayedIHashimi

Leave a Comment
  • Please add 7 and 4 and type the answer here:
  • Post
  • Would be interesting to see how easy/hard it is to change the scaffolding template.

  • Hi Peter, please see my response at blogs.msdn.com/.../how-to-customize-scaffolding-templates-for-asp-net-vnext.aspx

  • and why not use or build upon yeoman?

  • @Kenwarner in this case we are scaffolding new content into an existing project based on C# artifacts that the project consists of. We need to be able to inspect the existing content and then load their .NET types to generate new content based on that. That is why we are using the existing command support in ASP.NET vNext projects for scaffolding in this scenario.

    We are looking at yeoman for new project scenarios. We have already published some generators, take a look at one of our previous posts for more details blogs.msdn.com/.../develop-asp-net-vnext-applications-on-a-mac.aspx. Do you think we should continue investing in these generators?

Page 1 of 1 (4 items)