是伍尚金哇_v

ASP.NET Core Web API处理HttpResponseMessage类型返回值的问题

电脑版发表于:2020/3/24 19:38

这个问题如果不是被人问我我还真不知道有这么一个

通常我们需要在WebApi里返回json格式的数据,可以通过HttpResponseMessage的方式来处理,因为WebApi默认返回的是XML格式


FrameWork里的WebApi确实可以没问题,但是在Net Core里的WebApi

HttpResponseMessage处理返回json数据时得到一堆未知的东西


解决方法

安装 nuget 包  Microsoft.AspNetCore.Mvc.WebApiCompatShim

Startup -> ConfigureServices 中,在 AddMvc() 之后添加 AddWebApiConventions() 即可:代码如下


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

然后看看效果





关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢