How to create a controller using the dotnetcore command line

In Ruby on Rails, you can create controllers using something like the following on the command line:

$ rails generate controller ControllerName action1 action2 ...etc

Is there something similar in dotnetcore cli for creating controllers?

From what I can find, dotnetcore cli seems rather limited in the commands you can do. I found some of the Microsoft docs about the cli extension, but I'm not sure how to do this for such a team.

UPDATE

Using the @Sanket answer, I was able to create controllers for my dotnetcore application. However, I encountered an error

 Package Microsoft.Composition 1.0.27 is not compatible with netcoreapp1.1 (.NETCoreApp,Version=v1.1). Package Microsoft.Composition 1.0.27 supports: portable-net45+win8+wp8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile259) One or more packages are incompatible with .NETCoreApp,Version=v1.1. 

To solve this problem, I added "net451" to the frame import statement for the netcoreapp1.1 dependency.

My simple project.json file for my empty project (using the @Sanket project.json template) looks like this:

 { "version": "1.0.0-*", "buildOptions": { "debugType": "portable", "emitEntryPoint": true }, "dependencies": { "Microsoft.VisualStudio.Web.CodeGeneration.Tools": { "version": "1.1.0-preview4-final", "type": "build" }, "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": { "version": "1.1.0-preview4-final", "type": "build" }, "Microsoft.AspNetCore.Mvc": "1.0.0-*", "Microsoft.AspNetCore.StaticFiles": "1.0.0-*" }, "tools": { "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final", "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final", "Microsoft.VisualStudio.Web.CodeGeneration.Tools": { "version": "1.1.0-preview4-final", "imports": [ "portable-net45+win8" ] } }, "frameworks": { "netcoreapp1.1": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.1.0" } }, "imports": [ "netcoreapp1.1", "net451" ] } } } 

After starting (in the terminal) $ dotnet restore I can run the following command to create the base controller.

$ dotnet aspnet-codegenerator --project . controller -name SimpleController dotnet aspnet-codegenerator --project . controller -name SimpleController

This created an empty SimpleController.cs controller with the following code: (Note that my dotnet project was called ToolsAppDotNetCore )

 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; namespace ToolsAppDotNetCore { public class SimpleController : Controller { public IActionResult Index() { return View(); } } } 

@Sanket's answer contains more detailed information about the controller parameters generated by the code.

+10
source share
2 answers

If you use Command Prompt, you can get scaffold functions with the Code Generator package. To use this, you first need to include the CodeGeneration packages in project.json.

 "dependencies": { "Microsoft.VisualStudio.Web.CodeGeneration.Tools": { "version": "1.0.0-preview2-final", "type": "build" }, "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": { "version": "1.0.0-preview2-final", "type": "build" } }, "tools": { "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final", "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final", "Microsoft.VisualStudio.Web.CodeGeneration.Tools": { "version": "1.0.0-preview2-final", "imports": [ "portable-net45+win8" ] } } 

Now you can restore packages using the dotnet restore command. After it is completed, you can control the controllers and views with the following command -

 dotnet aspnet-codegenerator --project . controller -name HelloController -m Author -dc WebAPIDataContext 

The above command will generate a controller named HelloController in the root directory and views for the CRUD parameters inside the Hello folder in the Views folder. You can use the --help command line switch after the controller option to get more options about the controller generator.

+9
source

This is a new way from mid-2018.

You must install dotnet-aspnet-codegenerator.
Now this is done globally, and not through the Nuget package:

PowerShell:

 dotnet tool install --global dotnet-aspnet-codegenerator 

Then this is how you create a REST controller from an existing EF model in PowerShell:

 dotnet-aspnet-codegenerator -p "C:\MyProject\MyProject.csproj" controller -name MyDemoModelController -api -m My.Namespace.Models.MyDemoModel -dc MyDemoDbContext -outDir Controllers -namespace My.Namespace.Controllers 

Some useful calls

Show available generators ( -p... -h ):

 dotnet-aspnet-codegenerator -p "C:\MyProject\MyProject.csproj" -h 

Show available options for the controller generator ( -p... controller -h ):

 dotnet-aspnet-codegenerator -p "C:\MyProject\MyProject.csproj" controller -h 

Controller generation for many models in a loop

Here's how you can generate REST controllers for all models at a given path from PowerShell:

 Get-ChildItem "C:\MyProject\Models" -Filter *.cs | Foreach-Object { $scaffoldCmd = 'dotnet-aspnet-codegenerator ' + '-p "C:\MyProject\MyProject.csproj" ' + 'controller ' + '-name ' + $_.BaseName + 'Controller ' + '-api ' + '-m My.Namespace.Models.' + $_.BaseName + ' ' + '-dc MyDemoDbContext ' + '-outDir Controllers ' + '-namespace My.Namespace.Controllers' # List commands for testing: $scaffoldCmd # Excute commands (uncomment this line): #iex $scaffoldCmd } 
+2
source

Source: https://habr.com/ru/post/1260919/


All Articles