Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

MVC 6 Controller Scaffolding in Command Line

0.00/5 (No votes)
6 May 2015 1  
Controller Scaffolding in Visual Studio 2015 RC MVC 6 Project

Introduction

This tip discusses command line scaffolding in MVC 6 in Visual Studio 2015 RC.

Background

In this short tip, I will demonstrate how to do controller scaffolding in MVC 6 in the recent Visual Studio 2015 RC. CRUD scaffolding for controllers in MVC 6 in ASP.NET 5 has been moved to command line. In the latest RC of Visual Studio 2015, the .NET Execution Environment (DNX) has been renamed for k or kr. 

To start with the scaffolding process, please follow the article below to install DNVM to windows machine:

After installing dnvm, you can run from the command line you can run dnx command, for example to run the web site:

dnx . web

This will run the web site where the web command is defined in project.json file as below:

"commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls 
     http://localhost:5000"
}

Scaffolding Process

To create a new controller with CRUD operations can also be done with the dnx command from the prompt. To open the command prompt and go to your MVC 6 project folder where your project.json file is and apply the command to scaffold the new controller and the views as below:

dnx . gen controller -name NameOfController --dataContext DBContextName --model NameOfModel

Like web command, the gen command has also been defined in project.json as:

"commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000",
    "gen": "Microsoft.Framework.CodeGeneration"

  },

This is will create the controller and the views for CRUD operations.

In particular, I have used Person.cs model class and MyDBContext as the Database Context, the command was:

dnx . gen controller -name PersonController --dataContext MyDBContext --model Person

After running the command, it generates the controller and views as shown below:

Points of Interest

  • New feature of ASP.NET 5

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here