swagger – Swashbuckle ASP.NET Core使用application / x-www-f
发布时间:2020-12-16 07:42:17 所属栏目:asp.Net 来源:网络整理
导读:我有一个使用application / x-www-form-urlencoded的Action: [HttpPost("~/connect/token"),Consumes("application/x-www-form-urlencoded")]public async TaskIActionResult Exchange([FromBody]OpenIdConnectRequest request){ ..} 但Swashbuckle为Consum
我有一个使用application / x-www-form-urlencoded的Action:
[HttpPost("~/connect/token"),Consumes("application/x-www-form-urlencoded")] public async Task<IActionResult> Exchange([FromBody]OpenIdConnectRequest request) { .. } 但Swashbuckle为Consumes属性生成空数组.如果我将其更改为application / json,则会正确生成消耗数组. 它是与application / x-www-form-urlencoded相关的错误还是我需要另外配置Swashbuckle来支持这种应用程序类型? 解决方法
对于Swashbuckle而言,“消耗”不适合开箱即用,需要自定义扩展,就像
@domaindrivendev’s GitHub project的这一部分一样
有三个步骤: >创建参数属性 我将在my fork of the repo中添加更多说明,但这里是代码: 1. FromFormDataBodyAttribute.cs using System; using System.Collections.Generic; using System.Net.Http.Formatting; using System.Web.Http; using System.Web.Http.Controllers; using System.Web.Http.Validation; /// <summary> /// FromFormDataBody Attribute /// This attribute is used on action parameters to indicate /// they come only from the content body of the incoming HttpRequestMessage. /// </summary> [AttributeUsage(AttributeTargets.Parameter,Inherited = true,AllowMultiple = false)] public sealed class FromFormDataBodyAttribute : ParameterBindingAttribute { /// <summary> /// GetBinding /// </summary> /// <param name="parameter">HttpParameterDescriptor</param> /// <returns>HttpParameterBinding</returns> public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter) { if (parameter == null) throw new ArgumentNullException("parameter"); IEnumerable<MediaTypeFormatter> formatters = parameter.Configuration.Formatters; IBodyModelValidator validator = parameter.Configuration.Services.GetBodyModelValidator(); return parameter.BindWithFormatter(formatters,validator); } } 2 AddUrlFormDataParams.cs using Swashbuckle.Swagger; using System.Linq; using System.Web.Http.Description; /// <summary> /// Add UrlEncoded form data support for Controller Actions that have FromFormDataBody attribute in a parameter /// usage: c.OperationFilter<AddUrlFormDataParams>(); /// </summary> public class AddUrlFormDataParams : IOperationFilter { public void Apply(Operation operation,SchemaRegistry schemaRegistry,ApiDescription apiDescription) { var fromBodyAttributes = apiDescription.ActionDescriptor.GetParameters() .Where(param => param.GetCustomAttributes<FromFormDataBodyAttribute>().Any()) .ToArray(); if (fromBodyAttributes.Any()) operation.consumes.Add("application/x-www-form-urlencoded"); foreach (var headerParam in fromBodyAttributes) { if (operation.parameters != null) { // Select the capitalized parameter names var parameter = operation.parameters.Where(p => p.name == headerParam.ParameterName).FirstOrDefault(); if (parameter != null) { parameter.@in = "formData";//NB. ONLY for this 'complex' object example,as it will be passed as body JSON. //TODO add logic to change to "query" for string/int etc. as they are passed via query string. } } } } } 3更新Swagger.config //Add UrlEncoded form data support for Controller Actions that have FromBody attribute in a parameter c.OperationFilter<AddUrlFormDataParams>(); 4.在Controller方法中向参数添加属性 [FromFormDataBody]OpenIdConnectRequest request (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- API身份验证的最佳实践是什么?
- 如何在多个ASP.NET请求中更新EF中的实体而不再检索它?
- asp.net – 为什么当StateProvider不是InProc时,不会引发Se
- asp.net – Web API – Web窗体项目安全性错误
- asp.net-mvc-3 – MVC 3 WebGrid – 可以进行内联编辑吗?
- asp.net-mvc-4 – 如何使用ASP.NET MVC在Kendo UI Grid中实
- asp.net-mvc – asp.net mvc 3中DataAnnotations的行为是否
- asp.net – Oracle序列缓存老化过于频繁
- asp.net-mvc-4 – 为什么我们需要MVC中的Web API? mvc中re
- asp.net-mvc – ASP.NET MVC 6中的文件IO Close()方法错误
推荐文章
站长推荐
热点阅读