The ASP. NET Web Api Returns the result format, and the display in the browser

Source: Internet
Author: User

using System.Collections.Generic; using System.Web.Http; using ExtJS.WebApi.Data; namespace extjs.webapi.controllers{    [Routeprefix ("Api/book")] public    class Bookcontroller:apicontroller    {        [Route ("Get")] public         list<bookmodel> Get ()        {            return bookrepository.list;}}    }

Using Firefox, show:

The returned "Content-type" is "Application/xml"

ExtJS 4

    Ext.regmodel (' BookInfo ',   {       ' Name ''string ' }]   });    var bookstore = ext.create (' Ext.data.JsonStore ',    {        ' BookInfo ',        proxy: {            ' ajax ',             ' Book/get ',            ' JSON ' }}}    );

...

 {xtype:  ' combo ' , Fieldlabel:  book List ' , Listconfig: {loadingtext:  ' loading book information ' , empt            Ytext: }, TriggerAction:  ' all ' , Store:bookstore, Displayfield:  "Name" , Valuefield:  "N Ame ", Querymode:  ' remote ' , querydelay:300} 

The request succeeds, and the return data is XML

The data cannot be displayed in the drop-down box

IE no problem. The requested data is data in Application/json format

But in IE, the return is "Application/json"

In order to solve Firefox, request ASP. NET WEBAPI data, get the data of XML, need to do this:

 public  class  browserjsonformatter: Jsonmediatypeformatter {public  browserjsonformatter () {this . Supportedmediatypes.add (new  mediatypeheadervalue ( "text/html"             )); this .        serializersettings.formatting = formatting.indented; } public  override  void  Setdefaultcon Tentheaders (type type, httpcontentheaders headers, Mediatypeheadervalue mediatype) { Base .            Setdefaultcontentheaders (type, headers, mediatype); Headers. ContentType = new  mediatypeheadervalue ( "Application/json" ) {        CharSet =  "UTF-8" }; }    }

\app_start\webapiconfig.cs

         Public Static void Register (httpconfiguration config)        {           ...                       Config. Formatters.add (new browserjsonformatter ());        }

Request the API again

Reference:

Http://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome

ASP. NET Web Api Returns the result format, and the display in the browser

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.