WebApi2官网学习记录---Configuring
Configuration Settings
WebAPI中的configuration settings定义在HttpConfiguration中。有一下成员:
- DependencyResolver
- Filters
- Formatters
- IncludeErrorDetailPolicy
- Initializer
- MessageHandlers
- ParameterBindingRules
- Properties
- Routes
- Services
在ASP.NET Hosting中配置WebApi
namespace WebApplication1
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// TODO: Add any additional configuration code.
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
OWIN Self-Hosting中配置WebAPI
使用OWIN进行self-Hosting,需要创建一个HttpConfiguration实例,在这个实例中添加一些配置,然后将这个实例传给Owin.UseWebApi的扩展方法。
public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
}
}
Global Web API Services
HttpConfiguration.Services包含一些列的global services,WebAPI使用这些services执行各种各样的tasks,例如controller的选择以及content negotiation。
Services集合会被默认定义的services初始化,但是可以进行定义实现,一些services支持多个实例,一些仅支持一个实例。
支持单个实例的services
IActionValueBinder、IApiExplorer、IAssembliesResolver、IBodyModelValidator、IContentNegotiator、IDocumentationProvider、IHostBufferPolicySelector、IHttpActionInvoker、IHttpActionSelector、IHttpControllerActivator、IHttpControllerSelector、IHttpControllerTypeResolver、ITraceManager、ITraceWriter、IModelValidatorCache
支持多实例的services
IFilterProvider、ModelBinderProvider、ModelMetadataProvider、ModelValidatorProvider、ValueProviderFactory
将自定义的实现添加到支持多实例的service,可以使用Add和Insert的方式
config.Services.Add(typeof(IFilterProvider), new MyFilterProvider());
config.Services.Insert(typeof(IFilterProvider),0,new MyFilterProvider());
使用自定义的实现替换仅支持单实例的service
config.Services.Replace(typeof(ITraceWriter), new MyTraceWriter());
Per-Controller Configuration
使用per-controller方式可以重写如下设置:
Media-type formatters
Parameter binding rules
Services
通过自定义实现IControllerConfiguration接口的特性,然后应用到controller上可以实现上述目标using System;
using System.Web.Http;
using System.Web.Http.Controllers;namespace WebApplication1.Controllers
{public class UseMyFormatterAttribute : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings settings,
HttpControllerDescriptor descriptor)
{
// Clear the formatters list.
settings.Formatters.Clear(); // Add a custom media-type formatter.
settings.Formatters.Add(new MyFormatter());
}
} [UseMyFormatter]
public class ValuesController : ApiController
{
// Controller methods not shown...
}
}
IControllerConfiguration接口的作用:如果某个控制器是使用此接口的特性修饰的,则将调用此接口来初始化该控制器设置。
IControllerConfiguration.Initialize方法包含两个参数:
HttpControllerSettings这个对象是Configuration的一个子集,包含可以在per-controller时被重写的的对象HttpControllerDescriptor包含controller的描述信息
WebApi2官网学习记录---Configuring的更多相关文章
- WebApi2官网学习记录---Cookie
Cookie的几个参数: Domain.Path.Expires.Max-Age 如果Expires与Max-Age都存在,Max-Age优先级高,如果都没有设置cookie会在会话结束后删除cook ...
- WebApi2官网学习记录---批量处理HTTP Message
原文:Batching Handler for ASP.NET Web API 自定义实现HttpMessageHandler public class BatchHandler : HttpMess ...
- WebApi2官网学习记录---Html Form Data
HTML Forms概述 <form action="api/values" method="post"> 默认的method是GET,如果使用GE ...
- WebApi2官网学习记录--HttpClient Message Handlers
在客户端,HttpClient使用message handle处理request.默认的handler是HttpClientHandler,用来发送请求和获取response从服务端.可以在clien ...
- WebApi2官网学习记录--HTTP Message Handlers
Message Handlers是一个接收HTTP Request返回HTTP Response的类,继承自HttpMessageHandler 通常,一些列的message handler被链接到一 ...
- WebApi2官网学习记录--- Authentication与Authorization
Authentication(认证) WebAPI中的认证既可以使用HttpModel也可以使用HTTP message handler,具体使用哪个可以参考一下依据: 一个HttpModel可以 ...
- WebApi2官网学习记录---单元测试
如果没有对应的web api模板,首先使用nuget进行安装 例子1: ProductController 是以硬编码的方式使用StoreAppContext类的实例,可以使用依赖注入模式,在外部指定 ...
- WebApi2官网学习记录---Tracing
安装追踪用的包 Install-Package Microsoft.AspNet.WebApi.Tracing Update-Package Microsoft.AspNet.WebApi.WebHo ...
- WebApi2官网学习记录---异常处理
HttpResponseException 当WebAPI的控制器抛出一个未捕获的异常时,默认情况下,大多数异常被转为status code为500的http response即服务端错误. Http ...
随机推荐
- mybatis的简单使用
使用mybatis数据库时,需要添加一下jar包: asm-3.3.1.jarcglib-2.2.2.jarjavassist-3.17.1-GA.jarlog4j-1.2.17.jarmybatis ...
- HTML5 Canvas图片操作简单实例1
1.加载显示图片 <canvas id="canvasOne" class="myCanvas" width="500" height ...
- C#获取当前路径的几种方法
C#获取当前路径的方法如下: 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName -获取模块的完整路径. 2. ...
- winform 获取当前项目所在的路径
代码: string curAppPath = System.IO.Directory.GetParent(System.Environment.CurrentDirectory).Parent.Fu ...
- SQL存储过程笔记
一.概述 存储过程(Stored Procedure)是一组为了完成特定功能的SQL 语句集,经编译后存储在数据库.用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它. 优点: ...
- .net web api 的route理解
.NET web api 的特性是和MVC一样,通过Route 来控制action的访问方式.Route匹配规则是个奇特的方式,首先看一段Route的模板 Routes.MapHttpRoute( n ...
- jQuery操作元素
通常,我们在创建元素时,会使用以下代码: var p = document.createElement("p"); p.innerText = "this is para ...
- 总结JavaScript输出内容(document.write)
document.write() 可用于直接向 HTML 输出流写内容.简单的说就是直接在网页中输出内容. 第一种:输出内容用“”括起,直接输出""号内的内容.<script ...
- POJ1840 hash
POJ1840 问题重述: 给定系数a1,a2, ..,a5,求满足a1 * x1 ^ 3 + a2 * x2 ^ 3 +... + a5 * x5 ^ 3 = 0的 xi 的组数.其中ai, xi都 ...
- python文件_读取
1.文件的读取和显示 方法1: f=open(r'G:\2.txt') print f.read() f.close() 方法2: try: t=open(r'G:\2.txt') print t.r ...