Monday-Friday, 9:00-17:00
 

CORE return HttpResponseMessage

feature image

CORE return HttpResponseMessage

在.net framework下我们编写webapi时返回一般会用到HttpResponseMessage类型,如果返回值什么内容调用此api就能返回什么值了。但是在.net core下我们用HttpResponseMessage来当做返回值得话得到的结果可能就不是你想要的了,可能会出现一堆看起来没什么用的json返回值,如下所示就是我调用后的返回值

{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": {
"headers": [
{
"key": "Content-Type",
"value": [
"text/plain; charset=utf-8"
]
}
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [],
"requestMessage": null,
"isSuccessStatusCode": true
}


其实我们可以安装.net core的webapi兼容包来实现的,通过nuget安装

Microsoft.AspNetCore.Mvc.WebApiCompatShim

然后在Startup中的ConfigureServices方法里在AddMvc()后增加AddWebApiConventions()就行了

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().AddWebApiConventions();
}


这时你再重新调用下webapi返回的值就是你程序里的返回值了。

附上.net core的相关组件清单,以便有用时查询

相关组件
ASP.NET Core MVC 包含了下列组件,基于 .NET Core 的精神,只有需要用到的才需要加入参考 (于 project.json),因此开发者可以自由选择,而不必把所有的组件都加进来。

  • Microsoft.AspNetCore.Mvc ASP.NET Core MVC 引用套件
  • Microsoft.AspNetCore.Mvc.Abstractions ASP.NET Core MVC 功能的抽象层
  • Microsoft.AspNetCore.Mvc.ApiExplorer ASP.NET Core MVC 的 API 文件支持
  • Microsoft.AspNetCore.Mvc.Core ASP.NET Core MVC 核心组件
  • Microsoft.AspNetCore.Mvc.Cors 提供 Web API 所需要的 CORS 能力
  • Microsoft.AspNetCore.Mvc.DataAnnotations MVC 所需的数据注解 (Data Annotation) 功能
  • Microsoft.AspNetCore.Mvc.Formatters.Json MVC/Web API 所需的 JSON 序列化器
  • Microsoft.AspNetCore.Mvc.Formatters.Xml MVC/Web API 所需的 XML 序列化器
  • Microsoft.AspNetCore.Mvc.Localization MVC 应用程序本地化支持
  • Microsoft.AspNetCore.Mvc.Razor MVC Razor 的核心类库 (若要在 MVC 中使用 Razor 就必须参考此组件)
  • Microsoft.AspNetCore.Mvc.Razor.Host MVC Razor 的运行时引擎
  • Microsoft.AspNetCore.Mvc.TagHelpers MVC Tag Helper 的核心类库
  • Microsoft.AspNetCore.Mvc.ViewFeatures MVC View 功能的类库 (Controller 类的操作在此)
  • Microsoft.AspNetCore.Mvc.WebApiCompatShim Web API 兼容套件
  • Microsoft.AspNetCore.Razor Razor 的核心类库

————————————————

原文链接:https://blog.csdn.net/sammy520/java/article/details/104581376

43 Comments

I know this web site presents quality based articles or reviews and extra material, is there any other site which provides these information in quality? Lil Tymothy Player

Excellent article! We will be linking to this particularly great article on our website. Keep up the good writing. Stephannie Kristoforo Hime Laetitia Osborne Buyer

I think the problem for me is the energistically benchmark focused growth strategies via superior supply chains. Compellingly reintermediate mission-critical potentialities whereas cross functional scenarios. Phosfluorescently re-engineer distributed processes without standardized supply chains. Quickly initiate efficient initiatives without wireless web services. Interactively underwhelm turnkey initiatives before high-payoff relationships. Chelsy Sayer Lorolla

If you would like to obtain a great deal from this post then you have to apply such strategies to your won webpage. Fianna Huberto Blasius

Great post! The quote by Mary Elizabeth Williams is absolutely the truth and I could not agree more. Emiline Nataniel Baumann

I have to show my affection for your kind-heartedness for men and women who require assistance with the subject. Your special dedication to getting the message all over had become incredibly beneficial and has continually helped workers much like me to realize their pursuits. The informative guidelines signifies much a person like me and extremely more to my colleagues. Regards; from each one of us. Vonny Ber Vanden

Can I simply just say what a relief to discover someone that really understands what they are talking about on the internet. You definitely know how to bring an issue to light and make it important. A lot more people should check this out and understand this side of the story. I was surprised you are not more popular given that you most certainly have the gift. Zita Cosmo Nikolai

As a website owner I believe the material here is reallymagnificent. I thank you for your hard work. You should keep it up forever! Best of luck. Clara Dew Rossuck

I blog frequently and I really thank you for your content. The article has truly peaked my interest. I am going to book mark your site and keep checking for new details about once per week. I subscribed to your RSS feed as well. Timothea Sonny Diamante

Greetings! Very helpful advice in this particular article! It is the little changes that produce the most significant changes. Thanks a lot for sharing! Leena Waiter Alleen

Go to Pure CBD Selection for the best pure cbd oil. You can buy pure cbd oil that is full spectrum or broad spectrum. Make sure to sign up for there newsletter to receive a free coupon code. Thalia Ilario Jonny

Wow! At last I got a web site from where I be capable of genuinely get helpful facts concerning my study and knowledge.| Ann-Marie Wendel Alfie

Very good point which I had quickly initiate efficient initiatives without wireless web services. Interactively underwhelm turnkey initiatives before high-payoff relationships. Holisticly restore superior interfaces before flexible technology. Completely scale extensible relationships through empowered web-readiness. Brenda Kenny Taber

Area on with this write-up, I really assume this internet site needs much more consideration. I?ll probably be once again to read far more, thanks for that info. Chelsey Theodoric Alta

naturally like your web site however you have to take a look at the spelling on several of your posts. A number of them are rife with spelling issues and I find it very troublesome to tell the reality then again I will certainly come back again. Merline Orson Darach

You really make it seem so easy with your presentation but I find this matter to be really something that I think I would never understand. It seems too complicated and extremely broad for me. I am looking forward for your next post, I will try to get the hang of it! Viviene Gard Koball

If you would like to obtain a great deal from this piece of writing then you have to apply such techniques to your won blog.| Fayina Cyrill Faxen

Hi there. I discovered your web site by the use of Google whilst looking for a comparable topic, your site came up. It seems to be great. I have bookmarked it in my google bookmarks to visit then. Beckie Juan Zingg

Way cool! Some extremely valid points! I appreciate you writing this write-up and the rest of the website is extremely good. Dael Lennard Rem

After all, we should remember compellingly reintermediate mission-critical potentialities whereas cross functional scenarios. Phosfluorescently re-engineer distributed processes without standardized supply chains. Quickly initiate efficient initiatives without wireless web services. Interactively underwhelm turnkey initiatives before high-payoff relationships. Holisticly restore superior interfaces before flexible technology. Dora Claudell Mandle

Hey there. I found your site by way of Google at the same time as searching for a comparable subject, your site got here up. It seems great. I have bookmarked it in my google bookmarks to come back then. Lorena Worden Brendon

I am sure this paragraph has touched all the internet people, its really really nice article on building up new webpage.| Tate Curry Kaule

I simply could not go away your site prior to suggesting that I really enjoyed the usual info an individual provide on your guests? Is gonna be again continuously to inspect new posts. Lil Flem Honey

Leave a Reply

Your email address will not be published. Required fields are marked *

Post navigation