AspNetWebApi管线中如果定义两种类型的消息处理程序(全局/路由)
2013-09-09 11:27
399 查看
AspNetWebApi管线中如果定义两种类型的消息处理程序(全局/路由)
在AspNetWebApi管线中存在两种类型的消息处理程序(Message Handler)1.全局消息处理程序,所有的请求都将通过这些消息处理程序,全局的消息处理程序,通过HTTP配置的MessageHandlers.Add(消息处理程序),例如:
var config = New HttpConfiguration /HttpSelfHostHttpSelfHostConfiguration() config.MessageHandlers.Add( new CustomMessageHandler());
2.路由消息处理程序,只有特定的请求才被定制的消息处理程序处理。,可以通过配置添加新路由实现
这里以博文【WebApi应用】通过定制MessageHandler来添加对调用程序的授权验证 的demo为例
_config = new HttpSelfHostConfiguration("http://localhost:5555");
_config.Routes.MapHttpRoute( name:"CustomeRouteName", routeTemplate: "api2/{controller}/{username}", //新路由 defaults: new { username = RouteParameter.Optional }, constraints: null, handler: new CustomeMessageHandler //定制的消息处理程序 );
"api2/{controller}/{username}" 为新的路由URL模板,定制的消息处理程序(handler)只处理通过该UrlTemplate进行请求的消息
虽然定义了新的路由,然后运行程序:提示异常The inner handler has not been assigned
之所以执行新路由时候出现该异常是因为我们没有为定制的消息处理程序指定内部Http响应消息的处理程序,所以修改ValiteKeyMessageHandler类,在构造函数指定响应消息的处理程序.
public class ValiteKeyMessageHandler: DelegatingHandler { public string AppKey { get; set; } public ValiteKeyMessageHandler(string key, System.Web.Http.SelfHost.HttpSelfHostConfiguration configuration) { this.AppKey = key; //{ 此处为我们修改的 } //为定制的消息处理程序指定响应消息的内部处理程序 InnerHandler = new System.Web.Http.Dispatcher.HttpControllerDispatcher(configuration); } protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { if (!ValidateKey(request)) { var response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden); var task = new TaskCompletionSource<HttpResponseMessage>(); task.SetResult(response); return task.Task; } return base.SendAsync(request, cancellationToken); } private bool ValidateKey(HttpRequestMessage message) { var query = message.RequestUri.ParseQueryString(); string key = query["AppKey"]; return (AppKey == key); } }
然后重新编程程序正常执行
相关文章推荐
- 激活前一个程序(注册全局消息,使用Mutex探测,如果已经占用就广播消息通知第一个程序,然后第一个程序做出响应)
- 编译器错误消息: 类型“System.Data.Objects.DataClasses.EntityObject”在未被引用的程序集中定义。
- 编译器错误消息: CS0012: 类型“System.Data.Linq.DataContext”在未被引用的程序集中定义。必须添加对程序集“System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”的引用。
- 文件后缀与Mime类型对照表/由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。
- WPF 路由事件(RoutedEventArgs 事件消息、 RoutedEvent 路由事件、RoutedEventHandler路由事件处理程序、RaiseEvent引发路由事件)
- 编译器错误消息: CS0012: 类型 在未被引用的程序集中定义。必须添加对程序集 的引用
- 编译器错误消息: 类型“System.Data.Objects.DataClasses.EntityObject”在未被引用的程序集中定义。
- Js事件详解(1)事件类型及几种添加事件处理程序的方法
- Flex :HTTPService处理XML文件的两种方法, Asp生成Xml的方法,以及Flex定义变量、赋值的运用。。。。
- 用gcroot定义托管类型的全局变量
- 错误 类型“ESRI.ArcGIS.esriSystem.ISupportErrorInfo”在未被引用的程序集中定义。必须添加对程序集“ESRI.ArcGIS.System
- AspNet WebApi : MessageHandler(消息处理器 )
- 创建自定义路由处理程序(Creating a Custom Route Handler) | 定制路由系统| 高级路由特性 |精通ASP-NET-MVC-5-弗瑞曼
- dhl:类型“System.Data.Objects.DataClasses.EntityObject”在未被引用的程序集中定义必须添加对程序集“System.Data.Entity"引用
- 如何处理窗口叠加时的自定义消息路由
- BOM 创建同步(消息类型和处理代码)
- android 程序错误处理全局处理 UncaughtExceptionHandler
- 1、class或都id中含有“.”,“#”等特殊字符 在我们的程序中可能会遇到id中还有“.”和“#”等特殊字符,如果按照普通方式处理就会出错,解决方法是用转义符转义。 在下面程序中: <div
- PetShop程序中的购物车和订单处理模块(Profile技术,异步MSMQ消息)
- PetShop程序中的购物车和订单处理模块(Profile技术,异步MSMQ消息)