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

使用dotnet 命令在ASP.NET Core MVC 中建立Controller和View,以前讲解过使用yo 来建立Controller和View。web

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

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

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

 

建立ASP.NET Core应用程序

dotnet new -t web

dotnet restore

dotnet run

 

建立好之后,咱们来添加 Controller和View。rest

建立使用的dotnet命令为: dotnet aspnet-codegeneratorcode

 

建立Controller

1.空白Controller

dotnet aspnet-codegenerator controller -name LineZero -outDir Controllers

2.增删查改Controller

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

命令增长一个:-actions  blog

3.增删查改+EF Controller

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

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

-m 指的是model开发

-dc 指 DbContext generator

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+dnxcore50",
        "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 命令。

 

若是你以为本文对你有帮助,请点击“推荐”,谢谢。

相关文章
相关标签/搜索