Click here to Skip to main content
14,668,125 members
Articles » Web Development » ASP.NET » Howto
Tip/Trick
Posted 6 May 2015

Tagged as

Stats

28K views
10 bookmarked

MVC 6 Controller Scaffolding in Command Line

Rate this:
4.60 (5 votes)
Please Sign up or sign in to vote.
4.60 (5 votes)
6 May 2015CPOL
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:

Image 1

Points of Interest

  • New feature of ASP.NET 5

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mostafa Asaduzzaman
Software Developer (Senior)
Australia Australia
No Biography provided

Comments and Discussions

 
QuestionWhy '.' in dnx command Pin
techbhalla videosgeek6-Jul-16 22:41
Membertechbhalla videosgeek6-Jul-16 22:41 
GeneralMy vote of 5 Pin
Camilo Reyes6-May-15 9:20
professionalCamilo Reyes6-May-15 9:20 
This CLI scaffolding capability looks so cool!
GeneralRe: My vote of 5 Pin
Mostafa Asaduzzaman6-May-15 9:26
MemberMostafa Asaduzzaman6-May-15 9:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.