Asp.NetCore MVC Web 应用
Asp.NetCore MVC 与 普通的MVC 基本一致, 只是代码结构稍有改动
一、创建项目
1.

2.

3. 项目结构

二、 构建数据模型
1. Startup类中配置EF Core MySql (NuGet中下载 MySql.Data.EntityFrameworkCore)

2. 使用自带的依赖注入,注册EF Core MySql
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MovieContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("MovieContext")));
services.AddMvc();
}
3. Movie 类:
public class Movie
{
public int ID { get; set; } [Display(Name = "标题")]
[StringLength(, MinimumLength = )]
public string Title { get; set; } [Display(Name = "发布时间")]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; } [Display(Name = "类型")]
public string Genre { get; set; }
[Display(Name = "价格")]
public decimal Price { get; set; }
}
4. MovieContext 类:
public class MovieContext : DbContext
{
public MovieContext(DbContextOptions<MovieContext> options)
: base(options)
{
}
public DbSet<Movie> Movie { get; set; }
}
5 . 控制器:
public class MovieController : Controller
{
private readonly MovieContext _context;
public MovieController(MovieContext context)
{
_context = context;
} public IActionResult Index()
{
var movies = _context.Movie.ToList(); return View(movies);
} public IActionResult Create()
{
return View();
} //[Bind] 特性是防止过度发布的一种方法。 只应在 [Bind] 特性中包含想要更改的属性
//ValidateAntiForgeryToken 特性用于防止请求伪造,
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,Title,ReleaseDate,Genre,Price")] Movie movie)
{
if (ModelState.IsValid)
{
return NotFound();
}
try
{
_context.Add(movie);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
throw;
}
return RedirectToAction("Index");
} public IActionResult Edit(int id)
{
var movie = _context.Movie.Where(i => i.ID == id).FirstOrDefault(); return View(movie);
} //[Bind] 特性是防止过度发布的一种方法。 只应在 [Bind] 特性中包含想要更改的属性
//ValidateAntiForgeryToken 特性用于防止请求伪造,
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,Title,ReleaseDate,Genre,Price")] Movie movie)
{
if (id != movie.ID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(movie);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
throw;
}
return RedirectToAction("Index");
}
return View(movie);
}
}
6. 创建视图 Create
@model WebApp_Mvc.Models.Movie
@{
ViewData["Title"] = "Create";
} <h2>Create</h2> <form asp-action="Create">
<div class="form-horizontal">
<h4>Movie</h4>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="ID" />
<div class="form-group">
<label asp-for="Title" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="ReleaseDate" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="ReleaseDate" class="form-control" />
<span asp-validation-for="ReleaseDate" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="Genre" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Genre" class="form-control" />
<span asp-validation-for="Genre" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="Price" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Price" class="form-control" />
<span asp-validation-for="Price" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
</form> <div>
<a asp-action="Index">Back to List</a>
</div> @section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Edit:
@model WebApp_Mvc.Models.Movie
@{
ViewData["Title"] = "Edit";
} <h2>Edit</h2> <form asp-action="Edit">
<div class="form-horizontal">
<h4>Movie</h4>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="ID" />
<div class="form-group">
<label asp-for="Title" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="ReleaseDate" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="ReleaseDate" class="form-control" />
<span asp-validation-for="ReleaseDate" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="Genre" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Genre" class="form-control" />
<span asp-validation-for="Genre" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="Price" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Price" class="form-control" />
<span asp-validation-for="Price" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
</form> <div>
<a asp-action="Index">Back to List</a>
</div> @section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Index:
@model List<WebApp_Mvc.Models.Movie>
@{
} <h2>Index</h2> <p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model[0].Title)
</th>
<th>
@Html.DisplayNameFor(model => model[0].ReleaseDate)
</th>
<th>
@Html.DisplayNameFor(model => model[0].Genre)
</th>
<th>
@Html.DisplayNameFor(model => model[0].Price)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
<a asp-action="Details" asp-route-id="@item.ID">Details</a> |
<a asp-action="Delete" asp-route-id="@item.ID">Delete</a>
</td>
</tr>
}
</tbody>
</table>
编译运行:

Asp.NetCore MVC Web 应用的更多相关文章
- ASP.NETCORE MVC模块化
ASP.NETCORE MVC模块化编程 前言 记得上一篇博客中跟大家分享的是基于ASP.NETMVC5,实际也就是基于NETFRAMEWORK平台实现的这么一个轻量级插件式框架.那么今天我主要分享的 ...
- ASP.NET MVC Web API Post FromBody(Web API 如何正确 Post)
问题场景: ASP.NET MVC Web API 定义 Post 方法,HttpClient 使用 JsonConvert.SerializeObject 传参进行调用,比如 Web Api 中定义 ...
- ASP.NET MVC Web API For APP
近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过在浏览器中使用 JavaScr ...
- [译]ABP框架使用AngularJs,ASP.NET MVC,Web API和EntityFramework构建N层架构的SPA应用程序
本文转自:http://www.skcode.cn/archives/281 本文演示ABP框架如何使用AngularJs,ASP.NET MVC,Web API 和EntityFramework构建 ...
- 【转载】ASP.NET MVC Web API 学习笔记---联系人增删改查
本章节简单介绍一下使用ASP.NET MVC Web API 做增删改查.目前很多Http服务还是通过REST或者类似RESP的模型来进行数据操作的.下面我们通过创建一个简单的Web API来管理联系 ...
- Asp.net mvc web api 在项目中的实际应用
Asp.net mvc web api 在项目中的实际应用 前言:以下只是记录本人在项目中的应用,而web api在数据传输方面有多种实现方式,具体可根据实际情况而定! 1:数据传输前的加密,以下用到 ...
- ASP.NET MVC Web API 学习笔记---第一个Web API程序
http://www.cnblogs.com/qingyuan/archive/2012/10/12/2720824.html GetListAll /api/Contact GetListBySex ...
- 在 ASP.NET MVC Web 应用程序中输出 RSS Feeds
RSS全称Really Simple Syndication.一些更新频率较高的网站可以通过RSS让订阅者快速获取更新信息.RSS文档需遵守XML规范的,其中必需包含标题.链接.描述信息,还可以包含发 ...
- 实战 ASP.NET MVC Web API
实战 ASP.NET MVC Web API Web API 框架基于 ASP.NET MVC 框架开发,是一个面向 Http 协议的通信框架.相对于 WCF 而言,Web API 只面向于 Http ...
随机推荐
- 文件读取错误UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 884: invalid start byte
参考: https://segmentfault.com/q/1010000004268196/a-1020000004269556 ubuntu下Python3使用open('filename', ...
- 深入理解基于selenium的二次开发
对于做web端自动化测试的人来说,可能接触selenium比QTP还要多,但是我们在做基于selenium的二次开发的时候,经常会说到二次开发是为了易于维护,很多人可能不懂得维护的价值是什么,和到底要 ...
- Python实践练习:正则表达式查找
题目 编写一个程序,打开文件夹中所有的.txt 文件,查找匹配用户提供的正则表达式的所有行.结果应该打印到屏幕上. 代码 #!/usr/bin/python # -*- coding: UTF-8 - ...
- python greenlet 背景介绍与实现机制
最近开始研究Python的并行开发技术,包括多线程,多进程,协程等.逐步整理了网上的一些资料,今天整理一下greenlet相关的资料. 并发处理的技术背景 并行化处理目前很受重视, 因为在很多时候,并 ...
- vs2015 新特性
vs2015 新特性 自动属性的增强 http://www.kwstu.com/ArticleView/manong_201411200854239378
- D3D-GetBackBuffer &GetFrontBufferData 抓屏&D3D抓取GPU数据
HRESULT GetBackBuffer( [in] UINT iSwapChain, [in] UINT ...
- javascript 对象的扩展性
javascript 对象 的可扩展性 javascript 对象中的可扩展性指的是:是否可以给对象添加新属性.所有的内置对象和自定义对象显示的都是可扩展的,对于宿主对象,则有javascript 引 ...
- 小学生福利web及APP原型展示
332熊哲琛 320刘佳 原型作业地址 https://edu.cnblogs.com/campus/fzzcxy/2016SE/home work/2180 原型设计链接 https://modao ...
- CMD-SVN查看版本修改记录
[CMD-SVN查看版本修改记录] 问题:想查看某个版本的具体修做了哪些改动? 方法:svn diff -r r1:(r1-1) (filename) filename可选,如果加上就表示查看 ...
- ADT离线安装
1.安装eclipse软件.安装后点击HELP菜单,找到下面的Install New Software并点击. 2.之后会弹出一个对话框,然后我们点击add,接下来弹出ADD对话框,然后我们再点击ar ...