As the example code shown below, I am trying to access the get method by calling http://localhost:51292/API/MyWebAPI/GetData/?Name=John then I receive an error saying " The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource. "
public class MyData
{
int ID { get; set; }
}
public class MyDataDTO : MyData
{
string Name { get; set; }
}
public class MyWebAPIController : ApiController
{
[AllowAnonymous]
[HttpGet]
public IHttpActionResult GetData(MyDataDTO publicInfo)
{
return Ok(publicInfo);
}
}
I don't find any proper reason on why it requires the content type, maybe it has something to do with the API controller, or the routing? My routing is showen as below:
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "API/{controller}/{action}"
);
app.UseWebApi(config);
The solution was just to add [FromUri]
before the MyDataDTO
object type for the Get method, such:
[AllowAnonymous]
[HttpGet]
public IHttpActionResult GetData([FromUri] MyDataDTO publicInfo)
{
return Ok(publicInfo);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.