The ASP. NET Core Web API handles the issue of httpresponsemessage type return values

Source: Internet
Author: User
Tags call back

After migrating our web API from the. NET Framework to. NET Core 1.1, you encounter a problem.

Previously, a Web API with a value type of Httpresponsemessage was returned, such as:

[HttpGet]  Public Async Task Get () {    returnawait _httpclient.getasync ($" ... " );}

(Note: The purpose of this Web API is to directly return the results of the HttpClient call back-end Web API to the calling client)

It would have returned the business data in JSON format, and now the JSON serialized data for Httpresponsemessage itself was returned:

{  "version": {}, "  content": {},  "StatusCode": $,  "reasonphrase": "OK",  "headers": [],  " Requestmessage ": {    " version ": {},    " content ": null,    " method ": {" method ":" GET "},    " RequestUri ":" ",    "Headers": [],    "Properties": {}  },  "Issuccessstatuscode": true}

A donkey's head is not a horse's mouth.

The solution was later found on StackOverflow--asp.net Core httprequestmessage returns strange JSON message, which was successfully resolved with the method in question.

Here's how to fix it:

1) Add the following configuration in. csproj in order to install the NuGet package in step 2nd (ASP. NET Core 2.0 does not need to add this configuration)

< Packagetargetfallback >$ (packagetargetfallback);p ortable-net45+win8</packagetargetfallback> 

2) Install NuGet package Microsoft.AspNetCore.Mvc.WebApiCompatShim

3) in Startup---Configureservices, add addwebapiconventions () after Addmvc () to:

 Public void configureservices (iservicecollection services) {    services. Addmvc (). Addwebapiconventions ();}

The ASP. NET Core Web API handles the issue of httpresponsemessage type return values

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.