第一章 概述

URI 统一资源标识符

URL 统一资源定位符

http方法:get,post,put,delete,head等

状态码:100-199,请求已被接受;

200-299,成功状态;

300-399,重定向;

400-499,客户端错误;

500-599,服务端错误;

restful web api:roa,面向资源

特征:

1.采用URI标识资源

2.使用“链接”关联相关的资源

3.使用统一的接口

4.使用标准的HTTP方法

5.表示多种资源表示方式

6.无状态性

soap web service: rpc,面向功能

第二章 路由

2.1 asp.net 路由

2.1.1 请求URL与物理文件的分离

 var defaults = new RouteValueDictionary { { "name", "*" }, { "id", "*" } };
RouteTable.Routes.MapPageRoute("", "employees/{name}/{id}","~/default.aspx", true, defaults);

2.1.4 注册路由映射

   var defaults = new RouteValueDictionary { { "areacode", "" }, { "days",  }};
var constaints = new RouteValueDictionary { { "areacode", @"0\d{2,3}" }, { "days", @"[1-3]" } };
var dataTokens = new RouteValueDictionary { { "defaultCity", "BeiJing" }, { "defaultDays", } }; RouteTable.Routes.MapPageRoute("default", "{areacode}/{days}","~/weather.aspx", false, defaults, constaints, dataTokens);
            var constaints = new RouteValueDictionary { { "areacode", @"0\d{2,3}" }, { "days", @"[1-3]{1}" }, { "httpMethod", new HttpMethodConstraint("POST") } };

2.2 ASP.NET Web api 路由

具有自己的路由系统

第三章 消息处理管道

3.1 httpmessagehandler 管道 delegatinghandler,httpserver

3.2 web host 模式下的消息处理管道(asp.net 管道)

3.3 self  host 模式下的消息处理管道 httpbinging  httpselfhostserver

第四章 HttpController的激活

ApiController httpcontrollerdescriptor

第五章 Action的选择

httpactiondescriptor httpparameterdescriptor

第六章 特性路由

RouteAttribute

为路由变量设置约束

设置URI前缀,RoutePrefix

第七章 Model绑定(上篇)

1. 基于HttpRouteData的参数绑定

MODEL绑定机制来对目标Action的某个参数进行绑定。

   [ModelBinder]
[DataContract(Namespace = "http://www.artech.com/")]
public class DemoModel
{
[DataMember]
public int X { get; set; } [DataMember]
public int Y { get; set; } [DataMember]
public int Z { get; set; }
}
   [HttpGet]
[Route("action1/{x}/{y}/{z}")]
public DemoModel Action1(int x, int y, int z)
{
return new DemoModel { X = x, Y = y, Z = z };
} [HttpGet]
[Route("action2/{x}/{y}/{z}")]
public DemoModel Action2(DemoModel model)
{
return model;
} [HttpGet]
[Route("action3/{x}/{y}/{z}")]
public IEnumerable<DemoModel> Action3(DemoModel model1, DemoModel model2)
{
yield return model1;
yield return model2;
} [HttpGet]
[Route("action4/{model1.x}/{model1.y}/{model1.z}/{model2.x}/{model2.y}/{model2.z}")]
public IEnumerable<DemoModel> Action4(DemoModel model1, DemoModel model2)
{
yield return model1;
yield return model2;
}

2.基于查询字符串的参数绑定

第八章 Model绑定(下篇)

简单类型,复杂类型

集合,数组,字典绑定

第九章 参数的绑定

5个原生的httpparameterbinging:

1.ModelBinderParameterBinding

2. FormatterParameterBinding

FormUrlEncodedMediaTypeFormatter

  <script>
$(function () {
$("form").submit(function () {
$.ajax({
url: "http://localhost:3721/api/contacts",
type: "POST",
contentType: "application/x-www-form-urlencoded",
data: $("form").serialize()
});
return false;
});
});
</script>
   public void Post()
{
IEnumerable<MediaTypeFormatter> formatters = new MediaTypeFormatter[] { new FormUrlEncodedMediaTypeFormatter() };
FormDataCollection formData = this.Request.Content.ReadAsAsync<FormDataCollection>(formatters).Result;
foreach (var item in formData)
{
Console.WriteLine("{0,-12}: {1}", item.Key, item.Value);
}
}

JQueryMvcFormUrlEncodedFormatter :兼容任意类型

  IEnumerable<MediaTypeFormatter> formatters = new MediaTypeFormatter[] { new JQueryMvcFormUrlEncodedFormatter() };
Contact contact = this.Request.Content.ReadAsAsync<Contact>(formatters).Result;

3. HttpRequestParameterBinding

4.CancellationTokenParameterBinding

5.ErrorParameterBinding

第十章 参数的验证

10.1 几种参数验证方式

1. 手工验证绑定的参数(不推荐)

2. 使用ValidationAttribute特性

  public class Person
{
[Required(ErrorMessageResourceName = "Required",ErrorMessageResourceType = typeof(Resources))]
public string Name { get; set; } [Required(ErrorMessageResourceName = "Required",ErrorMessageResourceType = typeof(Resources))]
[Domain("M", "F", "m", "f", ErrorMessageResourceName = "Domain",ErrorMessageResourceType = typeof(Resources))]
public string Gender { get; set; } [Required(ErrorMessageResourceName = "Required",ErrorMessageResourceType = typeof(Resources))]
[Range(, , ErrorMessageResourceName = "Range",ErrorMessageResourceType = typeof(Resources))]
public int? Age { get; set; }
}

验证结果的自动响应:

   public class ValidateAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}
base.OnActionExecuting(actionContext);
}
}

第十一章 Action的执行

第十二章 过滤器

5种Filter类型:

AuthenticationFilter 认证

AuthorizationFilter 授权

ActionFilter 回调操作

利用自定义actionfilter实现对action方法执行结果的缓存(S1207)

ExceptionFilter 异常处理

OverrideFilter 屏蔽外层注册的Filter

第十三章 安全

1. iis/asp.net认证:

basic 认证:明文传输,不安全  (弹出windows登录界面)

digest 认证:只适合domain模式,不适合work group模式;哈希算法(md5)(弹出windows登录界面)

Windows集成认证(AD局域网),(不弹出windows登录界面):利用NTLM和kerberos协议

ntlm:nt lan manager 域控制器

kerberos:包含客户端,服务端密钥分发中心。kdc

Forms认证(web)

2. ssl/tls 非对称加密:

a.(消息的发送方采用公钥进行加密,接收方采用私钥进行解密)。

b. 数字签名(hash)。签名和检验。

数字证书(ca:认证权威机构)(是一种数字签名的声明)

微软提供的MakeCert.exe ;也可以利用IIS创建一个自我签名的证书,设置绑定端口

webapi使用HTTPS,

    public override void OnAuthorization(HttpActionContext actionContext)
{
//如果当前为HTTPS请求,授权通过
if (actionContext.Request.RequestUri.Scheme == Uri.UriSchemeHttps)
{
base.OnAuthorization(actionContext);
return;
} //对于HTTP-GET请求,将Scheme替换成https进行重定向
if (actionContext.Request.Method == HttpMethod.Get)
{
Uri requestUri = actionContext.Request.RequestUri;
string location = string.Format("https://{0}/{1}", requestUri.Host, requestUri.LocalPath.TrimStart('/'));
IHttpActionResult actionResult = new RedirectResult(new Uri(location), actionContext.Request);
actionContext.Response = actionResult.ExecuteAsync(new CancellationToken()).Result;
return;
} //采用其他HTTP方法的请求被视为Bad Request
actionContext.Response = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
ReasonPhrase = "SSL Required"
};
}

3.第三方认证:oauth2.0。安全令牌:access token。4种授权模式:1.implicit   2.authrization code 3.resource owner password credentials 4. client credential

第十四章 跨域资源共享

1.JSONP

2.采用ASP.NET WebApi 原生的机制实现跨域资源

第十五章 web api的调用

两种调用方式:

一种是ajax,一种是HttpClient

   HttpRequestMessage request1 = new HttpRequestMessage(HttpMethod.Get, "http://localhost:3721/api/demo/action1");
HttpRequestMessage request2 = new HttpRequestMessage(HttpMethod.Get, "http://localhost:3721/api/demo/action1");
HttpRequestMessage request3 = new HttpRequestMessage(HttpMethod.Get, "http://localhost:3721/api/demo/action1"); MyHttpClientHandler handler1 = new MyHttpClientHandler { AllowAutoRedirect = false, AutomaticDecompression = System.Net.DecompressionMethods.GZip };
MyHttpClientHandler handler2 = new MyHttpClientHandler { MaxAutomaticRedirections = };
MyHttpClientHandler handler3 = new MyHttpClientHandler { MaxAutomaticRedirections = }; HttpResponseMessage response1 = handler1.SendAsync(request1, new CancellationToken()).Result;
HttpResponseMessage response2 = handler2.SendAsync(request2, new CancellationToken()).Result;
HttpResponseMessage response3 = handler3.SendAsync(request3, new CancellationToken()).Result;

支持自动压缩

asp.net web api 2框架揭秘文摘的更多相关文章

  1. 感恩回馈,《ASP.NET Web API 2框架揭秘》免费赠送

      在继<WCF全面解析(上下册)>.<ASP.NET MVC 4框架揭秘>之后,我的另一本书<ASP.NET Web API 2框架揭秘>( 本书详细信息见< ...

  2. 《ASP.NET Web API 2框架揭秘》样章(PDF版本)

    <ASP.NET Web API 2框架揭秘>(详情请见<新作<ASP.NET Web API 2框架揭秘>正式出版>)以实例演示的方式介绍了很多与ASP.NET ...

  3. ASP.NET Web API 2框架揭秘

    ASP.NET Web API 2框架揭秘(.NET领域再现力作顶级专家精讲微软全新轻量级通信平台) 蒋金楠 著   ISBN 978-7-121-23536-8 2014年7月出版 定价:108.0 ...

  4. 《ASP.NET Web API 2框架揭秘》

    <ASP.NET Web API 2框架揭秘> 基本信息 作者: 蒋金楠 出版社:电子工业出版社 ISBN:9787121235368 上架时间:2014-7-5 出版日期:2014 年7 ...

  5. 新作《ASP.NET Web API 2框架揭秘》正式出版

    我觉得大部分人都是“眼球动物“,他们关注的往往都是目光所及的东西.对于很多软件从业者来说,他们对看得见(具有UI界面)的应用抱有极大的热忱,但是对背后支撑整个应用的服务却显得较为冷漠.如果我们将整个“ ...

  6. ASP.NET Web API 2 框架揭秘

    这不是一本传统意义上的入门书籍 任何 —本书都具有对应的受众群体,所以我不得不将这句话放在最前面,并且希望所有 打算购买此书的读者能够看到.如果你之前对As氵NET W山API(或者AsPNET MⅤ ...

  7. ASP.NET WEB API 2 框架揭秘 读书笔记(一)

    第一章 概述 主要内容是介绍Web的基本概念,Restfull的基本概念及特性.最后介绍创建简单WebApi程序的步骤. Web的基本概念 IP/TCP协议簇分层,分为两种 链路层->网络层-& ...

  8. ASP.NET Web API 开篇示例介绍

    ASP.NET Web API 开篇示例介绍 ASP.NET Web API 对于我这个初学者来说ASP.NET Web API这个框架很陌生又熟悉着. 陌生的是ASP.NET Web API是一个全 ...

  9. ASP.NET Web API 自定义MediaType实现jsonp跨域调用

    代码来自<ASP.NET Web API 2 框架揭秘>一书. 直接上代码: /// <summary> /// 自定义jsonp MediaType /// </sum ...

随机推荐

  1. Cassandra 的启动和初始化

    Cassandra常用命令 Cassandra启动过程详解[原创] Cassandra 的入口 CassandraDaemon 作为Cassandra的入口,做了以下几件事: load configu ...

  2. ApacheOFBiz的相关介绍以及使用总结(三)

    Ofbiz中还提供了一些基础性服务,可以直接用来使用,下面就简单介绍说明一下.   ofbiz邮件发送服务   ofbiz中提供发送邮件相关功能:sendMailFromScreen   contex ...

  3. combiner中使用状态模式

    mapreduce中的combine过程 hadoop的map过程执行完成后,每一个map都可能会产生大量的本地输出,Combiner的作用就是对map端的输出先做一次合并,减少在map和reduce ...

  4. 009:JSON

    一. MySQL JSON类型 1. JSON介绍 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级 ...

  5. JavaScript 数组some()和filter()

      some方法 array1.some(callbackfn[, thisArg]) 对数组array1中的每个元素调用回调函数callbackfn,当回调函数返回true或者遍历完所有数组后,so ...

  6. 检测SqlServer服务器性能

    通过性能监视器监视 Avg. Disk Queue Length   小于2 Avg. Disk sec/Read , Avg. Disk sec/Write  小于10ms 可以用数据收集器定时收集 ...

  7. SSH框架的简化(struts2、spring4、hibernate5)

    目的: 通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.本文提纲:本文通过一个新闻管理系统的实例来简化ssh框架的代码编写,功能包括查询数据库中 ...

  8. Vue源码(一)

    入口文件 src/core/instance/index.js 中可以看到 function Vue (options) { if (process.env.NODE_ENV !== 'product ...

  9. 初识python(python的安装与运行)

    python--“优雅”.“明确”.“简单”的哲学定位 一.python的安装(Windows环境下) 1.在python官网下载安装文件 python的官方网址:https://www.python ...

  10. mysql注入快速学习基础

    前言: sql注入想学好,学通.必须得了解一下基础的SQL 语句.这里我快速理一理 正文: 搭建环境建议下phpsduy快速搭建 select * from kasi select 字段名 from ...