in asp. net core 3.1 Я создаю пользовательскую модель Binder, и она отлично работает. в контроллере, когда я использую FromQuery со сложной моделью, я получаю Unsupported Media Type.
контроллер и класс:
public class myModel
{
public myModel()
{}
public int ProjectId { get; set; }
public int OwnerId { get; set; }
}
[HttpGet("project/list/GetData")]
public async Task<IActionResult> GetData([FromQuery]myModel model)
{
//some code
}
код запуска:
services.AddMvc().AddMvcOptions(options =>
{
options.AllowEmptyInputInBodyModelBinding = true;
IHttpRequestStreamReaderFactory readerFactory = services.BuildServiceProvider().GetRequiredService<IHttpRequestStreamReaderFactory>();
options.ModelBinderProviders.Insert(0, new DefaultsModelBinderProvider(options.InputFormatters, readerFactory));
});
мой класс BinderProvider:
public class DefaultsModelBinderProvider : IModelBinderProvider
{
private readonly IList<IInputFormatter> formatters;
private readonly IHttpRequestStreamReaderFactory readerFactory;
public DefaultsModelBinderProvider(IList<IInputFormatter> formatters, IHttpRequestStreamReaderFactory readerFactory)
{
this.formatters = formatters;
this.readerFactory = readerFactory;
}
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context.Metadata.ModelType.BaseType.IsAssignableFrom(typeof(MyRequest)))
return new DefaultsModelBinder(formatters, readerFactory);
return null;
}
и ModelBinder:
public class DefaultsModelBinder : IModelBinder
{
private BodyModelBinder defaultBinder;
public DefaultsModelBinder(IList<IInputFormatter> formatters, IHttpRequestStreamReaderFactory readerFactory)
{
defaultBinder = new BodyModelBinder(formatters, readerFactory);
}
public async Task BindModelAsync(ModelBindingContext bindingContext)
{
await defaultBinder.BindModelAsync(bindingContext);
if (!bindingContext.Result.IsModelSet) return; //<= problem is here,and IsModelSet was false
//some code
}
}
мой ответ:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
"title": "Unsupported Media Type",
"status": 415,
"traceId": "|a8d35c74-47e9398b95bd3d9f."
}
Я не смог найти проблему Как вы думаете, в чем проблема и что должно Я делаю, чтобы решить эту проблему? спасибо за вашу помощь