在 Ruby on Rails 中,您可以在命令行中使用如下内容生成控制器:
rails generate controller ControllerName action1 action2
...etc
dotnetcore cli中是否有类似的东西用于生成控制器?
据我所知,dotnetcore cli 在您可以执行的命令中似乎非常有限。我确实从 Microsoft 的文档中找到了一些关于扩展 cli 的内容,但我对如何为这样的命令执行此操作没有信心。
更新:2019 年 1 月 29 日
@Jspy的答案是自 2018 年中期以来使用 dotnetcore cmd 生成控制器的新方法。
更新:2016 年 12 月 21 日
使用@Sanket的答案,我能够为我的 dotnetcore 应用程序生成控制器。但是我遇到了一个错误
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.
为了解决这个问题,我为依赖项添加"net451"
了框架导入语句。netcoreapp1.1
project.json
我的空项目(使用@Sanket 的project.json
模板)的简单文件如下所示:
{
"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"
]
}
}
}
运行(在终端中)后,$
dotnet restore
我可以运行以下命令来生成基本控制器。
dotnet aspnet-codegenerator --project . controller -name SimpleController
这使用以下代码生成了一个空控制器SimpleController.cs:(请注意,我的 dotnet 项目被调用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();
}
}
}