您的位置:首页 > 编程语言 > ASP

ASP.NET Core开发-MVC 使用dotnet 命令创建Controller和View

2016-07-20 22:38 1221 查看
使用dotnet 命令在ASP.NET Core MVC 中创建Controller和View,之前讲解过使用yo 来创建Controller和View。

下面来了解dotnet 命令来创建Controller和View,功能更加强大,更加完整。

结合VS Code 使你能跨平台更好更快速的开发 ASP.NET Core MVC。

也就可以在 Linux 和Mac 中更好的开发ASP.NET Core 应用程序。

创建ASP.NET Core应用程序

dotnet new -t web

dotnet restore

dotnet run




创建好以后,我们来添加 Controller和View。

创建使用的dotnet命令为: dotnet aspnet-codegenerator



创建Controller

1.空白Controller

dotnet aspnet-codegenerator controller -name LineZero -outDir Controllers


2.增删查改Controller

dotnet aspnet-codegenerator controller -name LineZero -actions -outDir Controllers


命令增加一个:-actions

3.增删查改+EF Controller

生成增删查改的action 及对应的视图。

dotnet aspnet-codegenerator controller -name LineZeroController -m ApplicationUser -dc ApplicationDbContext -outDir Controllers


-m 指的是model

-dc 指 DbContext

4.API Controller

dotnet aspnet-codegenerator controller -name LineZeroController -m ApplicationUser -dc ApplicationDbContext -api -outDir Controllers


增加一个 -api 就代表是生成api controller

更多命令可以参考帮助:dotnet aspnet-codegenerator controller -?





创建View

1.创建View

可以指定什么操作,Create|Edit|Delete|Details|List ,必须填入对应的model

dotnet aspnet-codegenerator view Index List -m ApplicationUser -f -outDir Views\LineZero


这里我们就创建了一个名为:Index 操作为:List 的View

-f 如果存在该文件则覆盖文件。dotnet aspnet-codegenerator 默认是不覆盖文件。

更多命令可以参考帮助:dotnet aspnet-codegenerator view -?



至此命令的展示也就完了。

dotnet aspnet-codegenerator 来源

那么这个命令是从哪里来的呢?dotnet 默认是没有aspnet-codegenerator

在dependencies 节点添加如下:

"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 节点添加如下:

"tools": {
...
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
"version": "1.0.0-preview2-final",
"imports": [
"portable-net45+win8"
]
}
},


dotnet restore

这样也就可以使用 dotnet aspnet-codegenerator 。

上面dotnet new 创建的web 项目可以使用 dotnet aspnet-codegenerator ,是由于项目默认已经给我们添加了对应的节点及引用。

如果你是在Windows 下使用 VS 2015 开发的话,你将得到更好的体验。

使用 VS 2015 打开带有dotnet aspnet-codegenerator的项目,VS 会自动扩展,新增额外添加功能。

在项目右键会发现多了添加 控制器,在Views 文件夹下右键,会多一个 添加 视图。



选择以后就可以添加不同类型的Controller,这个其实也是调用dotnet aspnet-codegenerator 命令。



如果你觉得本文对你有帮助,请点击“推荐”,谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: