asp.net mvc api返回'该XML文件似乎没有与之关联的任何样式信息.文档树如下所示. [英] asp.net mvc api returning 'This XML file does not appear to have any style information associated with it. The document tree is shown below.'

查看:194
本文介绍了asp.net mvc api返回'该XML文件似乎没有与之关联的任何样式信息.文档树如下所示.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架制作了一个简单的net mvc Web api,当我进行api调用时,我在chrome中收到此错误:

I made a simple net mvc web api using entity framework, when I make an api call I am getting this error in chrome:

此XML文件似乎没有与之关联的任何样式信息.文档树如下所示.

This XML file does not appear to have any style information associated with it. The document tree is shown below.

在IE中不会出现此错误,而是api的结果导致下载json文件...我希望以这样的方式设置响应,即结果将出现在浏览器中,而不是作为下载,我该怎么做?

in IE this error does not appear, instead the result of the api leads to a download of a json file... I want the response to be set in such as way that the results will appear in the browser, and not as a download, how do I do this?

推荐答案

在WebApiConfig.cs中的config.MapHttpAttributeRoutes();

Add the following code inside WebApiConfig.cs under config.MapHttpAttributeRoutes();

            config.MapHttpAttributeRoutes();
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("text/html"));
            var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
            config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

Application_Start()函数中的Global.asax.cs添加以下内容

And Global.asax.cs inside Application_Start() function add the following

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

这篇关于asp.net mvc api返回'该XML文件似乎没有与之关联的任何样式信息.文档树如下所示.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆