简体   繁体   English

创建自定义 model 活页夹并从查询中使用时获取不受支持的媒体类型

[英]Get Unsupported Media Type when create custom model binder and use from query

in asp.net core 3.1 i create custom model binder,and it's work fine.在 asp.net 核心 3.1 中,我创建了自定义 model 活页夹,它工作正常。 in controller when i use FromQuery with complex model i get Unsupported Media Type.在 controller 中,当我将 FromQuery 与复杂的 model 一起使用时,我得到了不受支持的媒体类型。

controller and class: controller 和 class:

    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
        }

startup code:启动代码:

    services.AddMvc().AddMvcOptions(options =>
                {
                    options.AllowEmptyInputInBodyModelBinding = true;


                    IHttpRequestStreamReaderFactory readerFactory = services.BuildServiceProvider().GetRequiredService<IHttpRequestStreamReaderFactory>();
                    options.ModelBinderProviders.Insert(0, new DefaultsModelBinderProvider(options.InputFormatters, readerFactory));
                });

my BinderProvider class:我的 BinderProvider class:

     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;
            }

and 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
            }

        }

my response:我的回复:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
    "title": "Unsupported Media Type",
    "status": 415,
    "traceId": "|a8d35c74-47e9398b95bd3d9f." 
}

I couldn't find the problem What do you think is the problem and what should I do to solve this problem?我找不到问题 你认为问题是什么,我应该怎么做才能解决这个问题? thank's for your help感谢您的帮助

A 415 here means simply that the mime type of the request body doesn't have any applicable formatter in ASP.NET Core.此处的 415 仅表示请求正文的 mime 类型在 ASP.NET 核心中没有任何适用的格式化程序。 For example, out of the box, you get this if you send XML as the XML formatters are not included by default.例如,开箱即用,如果您发送 XML 作为 XML 格式化程序默认情况下不包括在内,您会得到这个。 If you add them in, then the error goes away, because ASP.NET Core then knows how to handle the application/xml mimetype, namely via sending it over to an XML serializer to deserialize it into an object.如果将它们添加进去,那么错误就会消失,因为 ASP.NET Core 然后知道如何处理application/xml mimetype,即通过将其发送到 XML 序列化程序以将其反序列化为 ZA8CFDE6331BD49EB2AC96F89666

None of that is applicable to anything coming FromQuery .这些都不适用于来自FromQuery的任何东西。 The query string doesn't have a mime type: it's just key-value pairs of strings.查询字符串没有 mime 类型:它只是字符串的键值对。 It's not clear how you're making the request or what the request actually looks like from the question, but to get this error, you're sending something in the body, and that something is off.目前尚不清楚您是如何提出请求的,或者从问题中请求实际上是什么样子的,但要得到这个错误,您在正文中发送了一些东西,并且有些东西是关闭的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM