2

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

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

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?

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
5chr
  • 127
  • 1
  • 2
  • 7
  • 1
    Can you provide your code? How are you returning the data? What is the content type showing up as in the console? – DVK May 27 '16 at 20:49

2 Answers2

4

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 }
            );

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

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
Abdulhakim Zeinu
  • 3,333
  • 1
  • 30
  • 37
0

A web api application does not have an inferface by default. When you call a web api, you should expect some data on the response body in a specified format to read on the client. Given that, the web api is returning the content formated as a xml and the browser shows it.

The message you have posted is a default message of the browser when you do not have a style for the xml content. It also happen on other platforms, given it is a message of the browser.

If you need to show a friendly interface, I suggest you change from asp.net web api application to an asp.net mvc application. You can have both on the same application, and in the case to show an user interface, just return a View.

Community
  • 1
  • 1
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194