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模型的更多相关文章

  1. ArcGIS案例学习笔记2_2_模型构建器和山顶点提取批处理

    ArcGIS案例学习笔记2_2_模型构建器和山顶点提取批处理 计划时间:第二天下午 背景:数据量大,工程大 目的:自动化,批处理,定制业务流程,不写程序 教程:Pdf/343 数据:chap8/ex5 ...

  2. Django:学习笔记(7)——模型进阶

    Django:学习笔记(7)——模型进阶 模型的继承 我们在面向对象的编程中,一个很重要的的版块,就是类的继承.父类保存了所有子类共有的内容,子类通过继承它来减少冗余代码并进行灵活扩展. 在Djang ...

  3. Django:学习笔记(6)——模型

    Django:学习笔记(6)——模型 快速上手 模型到底是什么呢?我们可以想,如果一张数据表的各个字段可以自动映射到一个类的各个属性,则每条记录对应这个类的一个对象.那我们通过类方法来操作对象(即表记 ...

  4. 2.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:

    转自:https://www.cnblogs.com/ssslinppp/p/4528892.html 个人认为,使用@ResponseBody方式来实现json数据的返回比较方便,推荐使用. 摘要 ...

  5. JVM学习笔记——内存模型篇

    JVM学习笔记--内存模型篇 在本系列内容中我们会对JVM做一个系统的学习,本片将会介绍JVM的内存模型部分 我们会分为以下几部分进行介绍: 内存模型 乐观锁与悲观锁 synchronized优化 内 ...

  6. JUC学习笔记——共享模型之管程

    JUC学习笔记--共享模型之管程 在本系列内容中我们会对JUC做一个系统的学习,本片将会介绍JUC的管程部分 我们会分为以下几部分进行介绍: 共享问题 共享问题解决方案 线程安全分析 Monitor ...

  7. JUC学习笔记——共享模型之内存

    JUC学习笔记--共享模型之内存 在本系列内容中我们会对JUC做一个系统的学习,本片将会介绍JUC的内存部分 我们会分为以下几部分进行介绍: Java内存模型 可见性 模式之两阶段终止 模式之Balk ...

  8. CSS学习笔记——盒模型,块级元素和行内元素的区别和特性

    今天本来打算根据自己的计划进行前端自动化的学习的,无奈早上接到一个任务需求需要新增一个页面.自从因为工作需要转前端之后,自己的主要注意力几 乎都放在JavaScript上面了,对CSS和HTML这方面 ...

  9. 3.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:

    转自:https://www.cnblogs.com/ssslinppp/p/4528892.html 概述 在文章:<[Spring学习笔记-MVC-3]SpringMVC返回Json数据-方 ...

随机推荐

  1. Linux用户态程序计时方式详解[转]

    转自: http://www.cnblogs.com/clover-toeic/p/3845210.html 前言 良好的计时器可帮助程序开发人员确定程序的性能瓶颈,或对不同算法进行性能比较.但要精确 ...

  2. ArcGIS Server远程处理服务器(环境设置)

    当使用ArcGIS Server做远程处理服务器执行影像处理操作时,提示ERROR 999999通用错误代码,如下: Start Time: Mon Jul 03 13:49:06 2017Distr ...

  3. Android中通过拨号调起应用的实现方式及特殊情况处理

    Android中有时我们会有这样的需求:通过拨号调起我们的程序.这个需求如何实现呢? 思路当然是在我们的应用中实现一个广播接收器(BroadcastReceiver),对打电话时系统发出的广播进行拦截 ...

  4. SQL 注入教程

    SQL 注入测评教程 1     准备 安装包:Burpsuit.Python27.sqlmap 2     安装配置 2.1    Burpsuit 1)       解压Burpsuit 2)   ...

  5. HTML5 本地存储Web Storage简单了解

    ​HTML5本地存储规范,定义了两个重要的API :Web Storage  和  本地数据库Web SQL Database. 本地存储Web Storage 实际上是HTML4的cookie存储机 ...

  6. 使用Scrapy自带的ImagesPipeline下载图片,并对其进行分类。

    ImagesPipeline是scrapy自带的类,用来处理图片(爬取时将图片下载到本地)用的. 优势: 将下载图片转换成通用的JPG和RGB格式 避免重复下载 缩略图生成 图片大小过滤 异步下载 . ...

  7. iOS-开发,拨打电话

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"te ...

  8. pptp协议的工作原理

    我的工作机是A,通信网卡是Aeth0, Appp0: 然后我的云主机是B, 通信的网卡是Beth0, Bppp0: 在网卡Bppp0上会不断地很清晰的数据包: 16:40:39.522917 IP 6 ...

  9. Chrome Extension & Dark Theme

    Chrome Extension & Dark Theme https://chrome.google.com/webstore/detail/eimadpbcbfnmbkopoojfekhn ...

  10. Docker实战系列一:初识Docker for Windows

    windows下安装Docker官网教程Install Docker for Windows Docker配置官网教程Get started with Docker for Windows