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

  1. WebApi2官网学习记录---Cookie

    Cookie的几个参数: Domain.Path.Expires.Max-Age 如果Expires与Max-Age都存在,Max-Age优先级高,如果都没有设置cookie会在会话结束后删除cook ...

  2. WebApi2官网学习记录---批量处理HTTP Message

    原文:Batching Handler for ASP.NET Web API 自定义实现HttpMessageHandler public class BatchHandler : HttpMess ...

  3. WebApi2官网学习记录---Html Form Data

    HTML Forms概述 <form action="api/values" method="post"> 默认的method是GET,如果使用GE ...

  4. WebApi2官网学习记录--HttpClient Message Handlers

    在客户端,HttpClient使用message handle处理request.默认的handler是HttpClientHandler,用来发送请求和获取response从服务端.可以在clien ...

  5. WebApi2官网学习记录--HTTP Message Handlers

    Message Handlers是一个接收HTTP Request返回HTTP Response的类,继承自HttpMessageHandler 通常,一些列的message handler被链接到一 ...

  6. WebApi2官网学习记录--- Authentication与Authorization

    Authentication(认证)   WebAPI中的认证既可以使用HttpModel也可以使用HTTP message handler,具体使用哪个可以参考一下依据: 一个HttpModel可以 ...

  7. WebApi2官网学习记录---单元测试

    如果没有对应的web api模板,首先使用nuget进行安装 例子1: ProductController 是以硬编码的方式使用StoreAppContext类的实例,可以使用依赖注入模式,在外部指定 ...

  8. WebApi2官网学习记录---Tracing

    安装追踪用的包 Install-Package Microsoft.AspNet.WebApi.Tracing Update-Package Microsoft.AspNet.WebApi.WebHo ...

  9. WebApi2官网学习记录---异常处理

    HttpResponseException 当WebAPI的控制器抛出一个未捕获的异常时,默认情况下,大多数异常被转为status code为500的http response即服务端错误. Http ...

随机推荐

  1. IDirect3DDevice9::Clear

    在绘制每一帧图形前都要先清空视区,即清空渲染目标表面上的视区矩形的内容:颜色缓冲区.深度缓冲区或者模板缓冲区. HRESULT Clear(  [in]  DWORD Count,           ...

  2. 一种用css实现图片在父框中等比缩放并垂直居中的办法

    一个网页中往往会有很多图片,而网站的编辑上传图片时可能并不一定按照为父框设定的那个宽高来传,这样图片往往会将父框撑开或者被父框截断.一种比较好的解决的办法是这样的: HTML代码结构: <div ...

  3. 17--Box2D使用(三、触摸交互)

    Box2D引擎与触摸的交互通过创建鼠标关节以及碰撞检测来得到触摸点下面的刚体,在根据触摸操作完成相应的功能.首先添加触摸响应函数声明 virtual void ccTouchesBegan(cocos ...

  4. Mysql中natural join和inner join的区别

    假设有如下两个表TableA,TableB TableA TableB Column1 Column2 Column1 Column3 1 2 1 3 TableA的Column1列名和TableB的 ...

  5. Apache配置rewrite

    最近将代码做了迁移,更换了web服务器,从原来的Nginx,换成使用Apache,多少有些区别.这里整理一下在apache下实现rewrite功能. 第一部分:修改apache配置文件支持rewrit ...

  6. 怎样使用pyinstaller打包

    安装好pyinstaller后 cd 到pyinstaller.py目录,在命令行输入:python pyinstaller.py 参数 主文件所在目录 如:python pyinstaller.py ...

  7. Javascript 原型注意事项

    function abc() {} abc.prototype.xx = { name: "keatkeat" } var x = new abc(); x.xx.name = & ...

  8. Loading Cargo

    Loading Cargo "Look Stephen, here's a list of the items that need to be loaded onto the ship. W ...

  9. library cache: mutex X

    我们先来看看 library cache: mutex X . 是个什么东西 The library cache mutex is acquired for similar purposes that ...

  10. 开机启动tomcat

    windows: 成功之后在dos窗口键入 service.bat install Tomcat 输完然后按Enter键,出现如下窗口,便成功了. 进入windows服务管理,设成是自动的. #chk ...