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

 

使用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 命令。

Core开发-MVC 使用dotnet 命令创建Controller和View的更多相关文章

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

    使用dotnet 命令在ASP.NET Core MVC 中创建Controller和View,之前讲解过使用yo 来创建Controller和View. 下面来了解dotnet 命令来创建Contr ...

  2. 创建ASP.NET Core MVC应用程序(1)-添加Controller和View

    创建ASP.NET Core MVC应用程序(1)-添加Controller和View 参考文档:Getting started with ASP.NET Core MVC and Visual St ...

  3. VisualStudioCode中用dotnet命令创建多个ASP.NET Core 项目、类库、控制台程序,并添加应用间的引用

    一.准备工作 首先安装VisualStudioCode并且可以使用. 1.首先新创建空的MyApps文件夹,作为项目主目录,下面将在这个文件夹中创建多个web应用程序.类型.控制台程序等. 2.打开V ...

  4. MVC 用基架创建Controller,通过数据库初始化器生成并播种数据库

    1 创建MVC应用程序 2 在Model里面创建实体类 using System; using System.Collections.Generic; using System.Linq; using ...

  5. mvc源码解读(20)-controller和view之查找view

    很多时候在mvc项目中我们需要去扩展自己的视图引擎,大概看起来应该下面这个样子的: public class RazorEngineExpand : RazorViewEngine { private ...

  6. .NET Core dotnet 命令大全

    dotnet 命令大全,让你理解dotnet 命令. 本文将以一个实例串起 dotnet 所有命令,让你玩转dotnet 命令. 本篇文章编写环境为windows 10 ,dotnet 命令同样适用于 ...

  7. Core dotnet 命令大全

    Core dotnet 命令大全 dotnet 命令大全,让你理解dotnet 命令. 本文将以一个实例串起 dotnet 所有命令,让你玩转dotnet 命令. 本篇文章编写环境为windows 1 ...

  8. [转].NET Core dotnet 命令大全

    本文转自:http://www.cnblogs.com/linezero/p/dotnet.html https://docs.microsoft.com/en-us/dotnet/articles/ ...

  9. windows/Linux下设置ASP.Net Core开发环境并部署应用

    10分钟学会在windows/Linux下设置ASP.Net Core开发环境并部署应用 创建和开发ASP.NET Core应用可以有二种方式:最简单的方式是通过Visual Studio 2017 ...

随机推荐

  1. sql从某不连续的数字中将其分段并找出缺失的数字并分段

    首先做准备数据 )) ') ') ') ') ') ') ') ') ') ') ') ') ') ') ') ') 将数据转换成应该处理的数据格式 ),colValue INT ) ) ,LEN(c ...

  2. 解决Qt4.8.6+VS2010运行程序提示 FTH: (6512): *** Fault tolerant heap shim applied to current process. This is usually due to previous crashes

    这个问题偶尔碰到两次,现在又遇上了,解决办法如下: 打开注册表,设置HKLM\Software\Microsoft\FTH\Enabled 为0 打开CMD,运行Rundll32.exe fthsvc ...

  3. PHP获取真实的网络IP

    function get_client_ip() { $ip = $_SERVER['REMOTE_ADDR']; if (isset($_SERVER['HTTP_CLIENT_IP']) & ...

  4. Google机器学习教程心得(一)

    Hello world Google Machine Learning Recipes 1 官方中文博客 http://chinagdg.org/2016/03/machine-learning-re ...

  5. ListView的Item点击事件(消息传递)

    转载请保留原文出处“http://my.oschina.net/gluoyer/blog”,谢谢! 您可以到博客的“友情链接”中,“程序猿媛(最新下载)*.*”下载最新版本,持续更新!当前版本,也可直 ...

  6. vs2005编译QT4.5版本

    1. 下载代码:qt-win-opensource-src-4.5.02. 设置环境变量 Add to PATH: ‘;%QTDIR%/bin’3. 输入cmd,启动到vs2005安装目录下,进入Mi ...

  7. de4dot命令 v2.0.3.3405

    de4dot v2.0.3.3405 Copyright (C) 2011-2013 [email]de4dot@gmail.com[/email] Latest version and source ...

  8. ajax跨域请求的方案

    $.get("@Hosts.Default.Www/api/XXXXX/Getxxx/"+@Model.UserId, function(data) { $("#tota ...

  9. JavaScript代码编写尝试使用Vanilla JS 或者Jquery插件

    From Here: http://vanilla-js.com/ Vanilla JS is a fast, lightweight, cross-platform frameworkfor bui ...

  10. 用Python写的一个多线程机器人聊天程序

    本人是从事php开发的, 近来想通过php实现即时通讯(兼容windows).后来发现实现起来特别麻烦, 就想到python.听说这家伙在什么地方都能发挥作用.所以想用python来做通讯模块...所 ...