How to use HttpRequestMessage.CreateResponse <HttpError> () in ASP.NET 5
I have an empty web API project, as well as a complete MVC project created using VS2015. I notice that the HttpRequestMessage is in System.Net.Http , this is normal, but not for CreateResponse below:
This code is from my middleware exception log, which also returns formatted json from previous web API 2.
var resp = request.CreateResponse<HttpError>(HttpStatusCode.InternalServerError, new HttpError("Internal Server Error. Reference: " + guid), config); I am trying to figure out a CreateResponse extension CreateResponse which is supposedly available in System.Net.Http.HttpRequestMessageExtensions , as well as the HttpError class. Both were previously in the Microsoft.AspNet.WebApi.Core package.
After looking at the ASP.NET 5 code, I believe that it is only available in Microsoft.AspNet.Mvc.WebApiCompatShim . However, it is not included in the draft MVC sample.
Questions:
What is the WebApiCompatShim package and can I use it safely in a project?
What is the correct path to CreateResponse from HttpRequestMessage ?
Update
This WebApiCompatShimWebSite shows that WebApiCompatShim is
to get Web API 2. * as behavior
So it seems just for the purpose of compatibility. I have to rebuild the code. Now I find that MVC does not have the function of message handlers, and it seems that ordinary middleware is the way to go.
If you add the Microsoft.AspNet.WebApi.Core NuGet package, you will get the extensions you are looking for.
link: https://www.nuget.org/packages/microsoft.aspnet.webapi.core/
This is an extension method of the HttpRequestMessage . You should reference the following DLL, which is included in the Microsoft.AspNet.WebApi.Core Nuget package:
Nuget\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll This time you can see the CreateResponse :
var response = request.CreateResponse(...) In accordance with:
https://docs.asp.net/en/latest/tutorials/first-web-api.html
You can achieve similar functionality by creating a new instance of ObjectResult (the status code can subsequently be changed by setting the StatusCode property).