11

When creating controller in MVC 6 I don't see the scaffolding for creating controller methods? Will they be missing or in the production release?

1

6 Answers 6

9

The namespaces have changed in Core 1.0

"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.0.0-preview1-final",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.0.0-preview1-final"

info from:

https://wildermuth.com/2016/05/17/Converting-an-ASP-NET-Core-RC1-Project-to-RC2

3
  • This is the correct answer !!! Just downloaded those dependencies and the "New Scaffoding Item" option appeared.
    – Olivertech
    Dec 23, 2016 at 20:03
  • On VS2015 Community using Asp.Net Core v1.1 I am seeing the CRUD scaffolding tools in the Context menu but they are greyed out and the Using Add > New Item gives me now Controller with View option either. Have restarted VS and machine to no avail.
    – K7Buoy
    Feb 2, 2017 at 14:56
  • I get a conflict error when trying to add this to a new project based on templates for Web API.
    – Edward
    Feb 13, 2017 at 1:32
7

If you are referring to CRUD scaffolding for controllers and views with ASP.NET 5 and MVC 6, it has been split off from the Visual Studio GUI and moved to command line.

You'll need a package called CodeGenerators, add it to your project.json configuration file as:

"dependencies": {
    ...
    "Microsoft.Framework.CodeGenerators.Mvc": "1.0.0-beta4",
    ...
},

At the moment VS does not offer GUI command to perform scaffolding but you can give a look to this link :

ASP.NET 5 (MVC6) Ground Up #4 - Scaffolding CRUD in MVC

3
  • "k gen" command does not exist in VS 2015 RC? what is the command for dnv?
    – Mostafa
    May 4, 2015 at 23:27
  • I found the command in VS 2015 RC for scaffolding: `dnx . gen controller -name NameOfController --dataContext DBContextName --model NameOfModel'
    – Mostafa
    May 4, 2015 at 23:44
  • Package Microsoft.Framework.CodeGenerators.Mvc 1.0.0-beta4 is not compatible with net452 (.NETFramework,Version=v4.5.2). Package Microsoft.Framework.CodeGenerators.Mvc 1.0.0-beta4 supports: - dnx451 (DNX,Version=v4.5.1) - dnxcore50 (DNXCore,Version=v5.0) One or more packages are incompatible with .NETFramework,Version=v4.5.2.
    – Edward
    Feb 13, 2017 at 1:21
2

Command line syntax for scaffolding controllers in MVC 6 is:

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

4
  • 2
    This is silly in my opinion. Why remove essential tooling and go command line? Jun 3, 2015 at 14:00
  • I have the same question, why?
    – Mostafa
    Jun 4, 2015 at 21:41
  • I have ask many times about why MS remove the script Bundling and Minification, and ms said :( ASP.NET 5 is complete rewrote and new gen web development.)
    – Cheung
    Jun 15, 2015 at 14:21
  • 1
    @CrazyCoderz I guess they wanted it to be cross-platform and don't depend on VS. But I'd expect support added to VS soon since it just needs to make a call to the code gen.
    – orad
    Jun 1, 2016 at 20:13
2

At least as of the latest Visual Studio 2015 Update 1, ASP.net 5 RC (update1) scaffolding is integrated into Visual Studio.

To do so include the following in your project.json file:

"dependencies": {
    ...
    "Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-*",
    ...

Then you can find it by right clicking the Controllers directory -> Add -> New Scaffolded Item

3
  • Hi, I did this but don't see the option like you mentioned. Also tried restarting VS. Any idea?
    – Sven
    Jun 15, 2016 at 22:12
  • @Sven Note the package to include depends on your VS/ASP version, see plumdog's answer for how to make it work with later versions.
    – Rick
    Jul 27, 2016 at 14:09
  • I downloaded this dependency and nothing happenned. There is no "New Sacaffodilg Item" created...
    – Olivertech
    Dec 23, 2016 at 20:00
1

It has been painful getting this to work in VS2015 following an upgraded to 1.1 but the following dependencies and tools in your project.json should work if you connecting to SQL to scaffold out your entities too.

{
 "dependencies": {
"Microsoft.EntityFrameworkCore": "1.1.0",
"Microsoft.EntityFrameworkCore.Design": "1.1.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
"Microsoft.VisualStudio.Web.CodeGeneration": "1.1.0-preview4-final",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.1.0-preview4-final",
"Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore": "1.1.0-preview4-final",
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.1.0-preview4-final"
},

"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
"Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4-final",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.1.0-preview4-final"}
}
0

Doing the following should resolve your issue.

  1. Open VS2015=>TOOLS=>Customize.
  2. Select Commands.
  3. Select Context menu.
  4. Select Project and Solution Context Menus|Folder|Add.
  5. Check that 'Controller' appears.
  6. Move up Controller item to the top.
  7. Restart Visual Studio.
2
  • This solution does nothing... only appears the Controller disabled.
    – Olivertech
    Dec 23, 2016 at 19:57
  • ya this didn't do anything
    – Kolby
    Feb 4, 2017 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.