简体   繁体   English

ASP.Net Core Web API:GET 请求不支持的媒体类型

[英]ASP.Net Core Web API: Unsupported Media Type on GET Request

I just started learning asp.net MVC6 and trying to learn how things work in it.我刚开始学习 asp.net MVC6 并试图了解它是如何工作的。 Currently, I'm using the .net 5.0.目前,我正在使用 .net 5.0。 So this is the WebAPI method and I just added a object parameter weatherForecast to it:所以这是 WebAPI 方法,我只是向它添加了一个对象参数weatherForecast

        [BindProperties(SupportsGet = true)]
    public class WeatherForecast
    {

        public int TemperatureC { get; set; }

        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

        public string Summary { get; set; }
    }

    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
{
    [HttpGet]
        public IEnumerable<WeatherForecast> Get(WeatherForecast weatherForecast)
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
}

When I sent this method a GET request from a browser or even from a POSTMAN:当我从浏览器甚至邮递员向此方法发送 GET 请求时:

http://localhost:1382/WeatherForecast?TemperatureC=12&Summary=HelloWorld

It returns:它返回:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
    "title": "Unsupported Media Type",
    "status": 415,
    "traceId": "00-a556c5fbdce0748bf3c7bd0e3200e92-21f4bf6470b534d-00"
}

The funny thing is in PostMan if I add a JSON body with a GET request (just for testing) then this works fine without any error.有趣的是在 PostMan 中,如果我添加一个带有 GET 请求的 JSON 主体(仅用于测试),那么这可以正常工作而不会出现任何错误。 I read the Microsoft model binding documentation [Here the link][1] and it says: "By default, properties are not bound for HTTP GET requests" so we have to use an attribute [BindProperty(SupportsGet = true)] or [BindProperties(SupportsGet = true)] and even after using this attribute it still doesn't work.我阅读了 Microsoft 模型绑定文档 [这里是链接][1],它说: “默认情况下,属性不绑定 HTTP GET 请求”所以我们必须使用属性[BindProperty(SupportsGet = true)][BindProperties (SupportsGet = true)]并且即使在使用此属性后它仍然不起作用。

So after digging down, I found an attribute [FromQuery] and after using this with the object parameter weatherForecast it starts working.所以在挖掘之后,我找到了一个属性[FromQuery]并且在将它与对象参数weatherForecast一起使用后,它开始工作。 But I want to know why it's not working without this attribute?但我想知道为什么没有这个属性就不能工作?

Normally in MVC5 if we don't specify any attribute with parameters like [FromUri] etc then the model binder automatically binds the parameters sent in the query string.通常在 MVC5 中,如果我们不使用[FromUri]等参数指定任何属性,那么模型绑定器会自动绑定在查询字符串中发送的参数。

What am I missing here?我在这里缺少什么? [1]: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-5.0#targets [1]: https : //docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-5.0#targets

You just have problems with routing values.您只是在路由值方面存在问题。 You are using MVC routing but trying to add query string too.您正在使用 MVC 路由,但也尝试添加查询字符串。 I suspect that your url hits a wrong action.我怀疑您的网址操作错误。

Since I can't see your controller, I can only recommend to add an attribute routing as the most reliable routing由于看不到你的控制器,所以只能建议添加一个属性路由作为最可靠的路由

    [Route("~/WeatherForecast/Get")]
    public IEnumerable<WeatherForecast> Get(WeatherForecast weatherForecast)

and use this url并使用这个网址

http://localhost:1382/WeatherForecast/Get?TemperatureC=12&Summary=HelloWorld

Update更新

Since you posted your controller, I recommed you to remove [Route("[controller]")] attribute, or if your startup config doesn't allow to do this change it to由于您发布了控制器,我建议您删除 [Route("[controller]")] 属性,或者如果您的启动配置不允许这样做,请将其更改为

    [ApiController]
    [Route("[controller]/[action]")]
    public class WeatherForecastController : ControllerBase

or for APIs usually或通常用于 API

[Route("api/[controller]/[action]")]

in this case you don't need attribute routing , but your urls pattern should be在这种情况下,您不需要属性路由,但您的网址模式应该是

http://localhost:1382/WeatherForecast/<action>
//or
http://localhost:1382/api/WeatherForecast/<action> //if api added

and forget about REST, it is good for a very simple textbook CRUDE only, in the real life each controller will have much more then 4 actions and you will not be able to find ehough gets and posts.忘记 REST,它仅适用于非常简单的教科书 CRUDE,在现实生活中,每个控制器将有超过 4 个动作,并且您将无法找到足够的获取和发布。

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

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