asp.net – Owin Middleware vs ExceptionHandler vs HttpMessag
发布时间:2020-12-15 22:34:57 所属栏目:asp.Net 来源:网络整理
导读:请任何人给我一些想法,以下三个模块在asp.net Web API 2.1中一起工作 Owin中间件 HttpMessageHandler(或DelegatingHandler) ExceptionHandler 我想要做的是开发和提供一个不变格式的json数据的web api,意味着如果实际的数据是 {"Id":1,"UserName":"abc","Ema
请任何人给我一些想法,以下三个模块在asp.net Web API 2.1中一起工作
> Owin中间件 我想要做的是开发和提供一个不变格式的json数据的web api,意味着如果实际的数据是 {"Id":1,"UserName":"abc","Email":"abc@xyz.com"} 然后我喜欢提供json {__d:{"Id":1,"Email":"abc@xyz.com"},code:200,somekey: "somevalue"} 为此,我尝试使用自定义ActionFilterAttribute,但我觉得(仍然不能确认),如果代码遇到异常,这样做不能提供类似的格式数据 请建议我最好的方向. 这是我的简短的自定义属性代码片段.还建议我是自定义属性是好的目的 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class,Inherited = true,AllowMultiple = false)] public class ResponseNormalizationAttribute : ActionFilterAttribute { public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { base.OnActionExecuted(actionExecutedContext); var response = actionExecutedContext.Response; object contentValue; if (response.TryGetContentValue(out contentValue)) { var nval = new { data=contentValue,status = 200 }; var newResponse = new HttpResponseMessage { Content = new ObjectContent(nval.GetType(),nval,new JsonMediaTypeFormatter()) }; newResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); actionContext.Response = newResponse; } } } 解决方法
如果您不使用Owin中间件,则可以全局包装所有响应,以便使用委托处理程序返回常量格式的json数据.
编写一个继承自DelegatingHandler的自定义处理程序: public class ApiResponseHandler : DelegatingHandler { protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,CancellationToken cancellationToken) { var response = await base.SendAsync(request,cancellationToken); return BuildResponse(request,response); } private static HttpResponseMessage BuildResponse(HttpRequestMessage request,HttpResponseMessage response) { object content; string errorMessage = null; if (response.TryGetContentValue(out content) && !response.IsSuccessStatusCode) { HttpError error = content as HttpError; if (error != null) { content = null; errorMessage = error.Message; } } var newResponse = request.CreateResponse(response.StatusCode,new ApiResponse((int)response.StatusCode,content,errorMessage)); foreach (var header in response.Headers) { newResponse.Headers.Add(header.Key,header.Value); } return newResponse; } } //ApiResponse is your constant json response public class ApiResponse { public ApiResponse(int statusCode,object content,string errorMsg) { Code = statusCode; Content = content; Error = errorMsg; Id = Guid.NewGuid().ToString(); } public string Error { get; set; } //your actual data is mapped to the Content property public object Content { get; set; } public int Code { get; private set; } public string Id { get; set; } } 在WebApiConfig.cs中注册处理程序: public static void Register(HttpConfiguration config) { // Web API routes config.MapHttpAttributeRoutes(); ... config.MessageHandlers.Add(new ApiResponseHandler()); config.Routes.MapHttpRoute( name: "DefaultApi",routeTemplate: "{controller}/{id}",defaults: new { id = RouteParameter.Optional } ); ... } 我在SO中发布了一个类似的答案,但这是在.NET Core中,并被实现为一个OWIN中间件(因为DelegatingHandler在.NET Core中). How can I wrap Web API responses(in .net core) for consistency? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-web-api2 – 如何创建HttpRequestHeaders类的实例
- 从零开始学 ASP.NET Core 与 EntityFramework Core 课程介绍
- asp.net-mvc – 使用令牌重置Identity 2.0 sms密码
- ASP.NET和底层连接已关闭:无法为SSL / TLS安全通道建立信任
- entity-framework – ASP .Net MVC与Java的[Transient]属性
- asp.net-mvc-3 – MVC视图的命名空间问题 – Razor引擎
- asp.net gridview中的datetime格式
- 为ASP.NET应用程序实现后台服务的最佳方法是什么?
- asp.net-mvc – ASP.NET MVC 6中的文件IO Close()方法错误
- ASP.NET网站攻击:如何回应?
推荐文章
站长推荐
- asp.net – iTextSharp可以栅格化/导出为JPEG或其
- 在ASP.NET应用程序中使用out-of-process会话状态
- asp.net-mvc – 删除Id MVC 5
- asp.net-web-api – WebAPI:403在发布网站后被禁
- asp.net-mvc – 难以启动基本单元测试(样本来自我
- ASP.NET MVC主页变量范围
- asp.net – 如何使用GridView和ObjectDataSource
- asp.net-mvc – 如何将第二个应用程序添加到MVC网
- ASP.NET MVC 3如何将Javascript注入主布局
- asp.net – 在JavaScript中访问asp:hiddenfield
热点阅读