DotNet 学习笔记 MVC模型
Model Binding Below is a list of model binding attributes:
•[BindRequired]: This attribute adds a model state error if binding cannot occur.
•[BindNever]: Tells the model binder to never bind to this parameter.
•[FromHeader], [FromQuery], [FromRoute], [FromForm]: Use these to specify the exact binding source you want to apply.
•[FromServices]: This attribute uses dependency injection to bind parameters from services.
•[FromBody]: Use the configured formatters to bind data from the request body. The formatter is selected based on content type of the request.
•[ModelBinder]: Used to override the default model binder, binding source and name. Model Validation •[CreditCard]: Validates the property has a credit card format.
•[Compare]: Validates two properties in a model match.
•[EmailAddress]: Validates the property has an email format.
•[Phone]: Validates the property has a telephone format.
•[Range]: Validates the property value falls within the given range.
•[RegularExpression]: Validates that the data matches the specified regular expression.
•[Required]: Makes a property required.
•[StringLength]: Validates that a string property has at most the given maximum length.
•[Url]: Validates the property has a URL format. services.AddMvc(options => options.MaxModelValidationErrors = ); Remote validation
public class User
{
[Remote(action: "VerifyEmail", controller: "Users")]
public string Email { get; set; }
} [AcceptVerbs("Get", "Post")]
public IActionResult VerifyEmail(string email)
{
if (!_userRepository.VerifyEmail(email))
{
return Json(data: $"Email {email} is already in use.");
} return Json(data: true);
}
Formatting Response Data
// GET api/authors/version
[HttpGet("version")]
public string Version()
{
return "Version 1.0.0";
} // GET api/authors/ardalis
[HttpGet("{alias}")]
public Author Get(string alias)
{
return _authorRepository.GetByAlias(alias);
} Startup.cs
services.AddMvc(options =>
{
options.RespectBrowserAcceptHeader = true; // false by default
} public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddXmlSerializerFormatters(); services.AddScoped<IAuthorRepository, AuthorRepository>();
}
OR
services.AddMvc(options =>
{
options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
}); Forcing a Particular Format
[Produces("application/json")]
public class AuthorsController Special Case Formatters
services.AddMvc(options =>
{
options.OutputFormatters.RemoveType<TextOutputFormatter>();
options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
}); Response Format URL Mappings
[FormatFilter]
public class ProductsController
{
[Route("[controller]/[action]/{id}.{format?}")]
public Product GetById(int id) /products/GetById/ ---The default output formatter
/products/GetById/.json ---The JSON formatter (if configured)
/products/GetById/.xml ---The XML formatter (if configured)
DotNet 学习笔记 MVC模型的更多相关文章
- ArcGIS案例学习笔记2_2_模型构建器和山顶点提取批处理
ArcGIS案例学习笔记2_2_模型构建器和山顶点提取批处理 计划时间:第二天下午 背景:数据量大,工程大 目的:自动化,批处理,定制业务流程,不写程序 教程:Pdf/343 数据:chap8/ex5 ...
- Django:学习笔记(7)——模型进阶
Django:学习笔记(7)——模型进阶 模型的继承 我们在面向对象的编程中,一个很重要的的版块,就是类的继承.父类保存了所有子类共有的内容,子类通过继承它来减少冗余代码并进行灵活扩展. 在Djang ...
- Django:学习笔记(6)——模型
Django:学习笔记(6)——模型 快速上手 模型到底是什么呢?我们可以想,如果一张数据表的各个字段可以自动映射到一个类的各个属性,则每条记录对应这个类的一个对象.那我们通过类方法来操作对象(即表记 ...
- 2.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:
转自:https://www.cnblogs.com/ssslinppp/p/4528892.html 个人认为,使用@ResponseBody方式来实现json数据的返回比较方便,推荐使用. 摘要 ...
- JVM学习笔记——内存模型篇
JVM学习笔记--内存模型篇 在本系列内容中我们会对JVM做一个系统的学习,本片将会介绍JVM的内存模型部分 我们会分为以下几部分进行介绍: 内存模型 乐观锁与悲观锁 synchronized优化 内 ...
- JUC学习笔记——共享模型之管程
JUC学习笔记--共享模型之管程 在本系列内容中我们会对JUC做一个系统的学习,本片将会介绍JUC的管程部分 我们会分为以下几部分进行介绍: 共享问题 共享问题解决方案 线程安全分析 Monitor ...
- JUC学习笔记——共享模型之内存
JUC学习笔记--共享模型之内存 在本系列内容中我们会对JUC做一个系统的学习,本片将会介绍JUC的内存部分 我们会分为以下几部分进行介绍: Java内存模型 可见性 模式之两阶段终止 模式之Balk ...
- CSS学习笔记——盒模型,块级元素和行内元素的区别和特性
今天本来打算根据自己的计划进行前端自动化的学习的,无奈早上接到一个任务需求需要新增一个页面.自从因为工作需要转前端之后,自己的主要注意力几 乎都放在JavaScript上面了,对CSS和HTML这方面 ...
- 3.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:
转自:https://www.cnblogs.com/ssslinppp/p/4528892.html 概述 在文章:<[Spring学习笔记-MVC-3]SpringMVC返回Json数据-方 ...
随机推荐
- This content database has a schema version which is not supported in this farm.
I want to move the website to another server. The new server has reinstall Sharepoint2013 serv ...
- 『JavaScript』封装
封装可以被定义为对对象的内部数据表现形式和实现细节进行隐藏.通过封装可以强制实施信息隐藏. 在JavaScript中,并没有显示的声明私有成员的关键字等.所以要想实现封装/信息隐藏就需要从另外的思路出 ...
- github简单使用教程(转)
github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.对于一般人来说公共仓库就已经足够了,而且我们也没多少代码来管理,O(∩_∩)O ...
- 给虚拟机发送键盘按键key
使用举例:virsh send-key 11 KEY_LEFTCTRL KEY_LEFTALT KEY_DELETE作用:发送"ctrl+alt+del"给虚拟机,linux虚拟机 ...
- LeetCode 237 ——删除链表中的结点
1. 题目 2. 解答 因为给定的只有一个待删除的结点指针,我们并不知道其前面结点,所以需要将待删除结点后面的结点值复制到前面结点去,然后指向其后的第二个结点即可. /** * Definition ...
- c# 复选下拉框
引用dll: http://pan.baidu.com/s/1qXa97UO 自定义类: namespace TMI_S { /// <summary> /// 功能描述:自定义多选下拉框 ...
- 软工实践Beta冲刺(1/7)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...
- 使用gradle打包时将依赖也合并入jar包
有些情形需要将项目依赖的jar包也合并入自己项目的jar包内,出来的这个jar我们称它为fat-jar.我所使用的方法需要用到shadow插件: 下面例子展示将mybatis打入我的jar包,并将my ...
- 团队作业4——第一次项目冲刺(Alpha版本)-第二篇
项目冲刺——第二阶段 度过了敏捷冲刺,各个成员积极汇报各自的工作.好了,着手下一步规划! Mission 团队成员 任务 郭达 实现PHP后台的答题判分查看正确率 刘德培 编写博客 石浩洋 实现 ...
- sping事务的理解
阅读数:2020 一.事务的基本原理 Spring事务的本质其实就是数据库对事务的支持,没有数据库的事务支持,spring是无法提供事务功能的.对于纯JDBC操作数据库,想要用到事务,可以按照以下步骤 ...