gpt4 book ai didi

c# - asp.net mvc api 返回 'This XML file does not appear to have any style information associated with it. The document tree is shown below.'

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:42 27 4
gpt4 key购买 nike

我使用 Entity Framework 制作了一个简单的 net mvc web api,当我进行 api 调用时,我在 chrome 中收到此错误:

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

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

最佳答案

config.MapHttpAttributeRoutes() 下的 WebApiConfig.cs 中添加以下代码;

            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 添加以下内容

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

关于c# - asp.net mvc api 返回 'This XML file does not appear to have any style information associated with it. The document tree is shown below.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37492010/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com