ASP.NET Web API用户选择的内容类型

asp.net web api user selected content type
2020-11-21
  •  译文(汉语)
  •  原文(英语)

Asp.net的默认返回类型是XML.但是我可以在配置设置中更改它.

public static void Register(HttpConfiguration config)
{ 
    config.Formatters.Clear();
    // config.Formatters.Add(new XmlMediaTypeFormatter());
    config.Formatters.Add(new JsonMediaTypeFormatter());
}

我的控制器是:

public class ProductController: ApiController
{
    public IEnumerable<Product> Get()
    {
        return new List<Product> {
           new Product {Name = "p1", Price = 10}, 
           new Product {Name = "p2", Price = 20}
        };
    }
}

现在我要这样:

  • 用户应使用参数指定返回类型.
  • http://domain/product/get (格式xml或json)

我不想更改控制器动作.

有什么方法可以通过Route参数或任何其他级别执行此操作?

解决过程1

默认情况下,不指定格式化程序,Web api将返回xml或json.

如果您需要返回json,只需在客户端的标头中指定以下内容:

Accept: application/json

ASP.NET Web API用户选择的内容类型

Java脚本

   var urlString = "http://localhost/api/values/Get";

    $.ajax({
        url: urlString,
        type: 'GET',
        data: {id : 1},
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) { console.log(data); }
    });

或xml:

Accept: application/xml

ASP.NET Web API用户选择的内容类型

Java脚本

var urlString = "http://localhost/api/values/Get";

$.ajax({
    url: urlString,
    type: 'GET',
    data: {id : 1},
    dataType: 'xml',
    contentType: 'application/xml',
    success: function (data) { console.log(data); }
});

Asp.net default return type is XML. But I can change that in config settings.

public static void Register(HttpConfiguration config)
{ 
    config.Formatters.Clear();
    // config.Formatters.Add(new XmlMediaTypeFormatter());
    config.Formatters.Add(new JsonMediaTypeFormatter());
}

My controller is:

public class ProductController: ApiController
{
    public IEnumerable<Product> Get()
    {
        return new List<Product> {
           new Product {Name = "p1", Price = 10}, 
           new Product {Name = "p2", Price = 20}
        };
    }
}

Now I want to this:

  • User should specify return type with a parameter.
  • http://domain/product/get (format xml or json)

I do not want to change my controller action.

Is there any way to do this with Route parameter or any other level?

Solutions1

By default without specifying a formatter the web api will return xml or json.

If you need to return json you simply specify the following from the client in the header:

Accept: application/json

asp.net web api user selected content type

Javascript

   var urlString = "http://localhost/api/values/Get";

    $.ajax({
        url: urlString,
        type: 'GET',
        data: {id : 1},
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) { console.log(data); }
    });

Or xml:

Accept: application/xml

asp.net web api user selected content type

Javascript

var urlString = "http://localhost/api/values/Get";

$.ajax({
    url: urlString,
    type: 'GET',
    data: {id : 1},
    dataType: 'xml',
    contentType: 'application/xml',
    success: function (data) { console.log(data); }
});
转载于:https://stackoverflow.com/questions/28362283/asp-net-web-api-user-selected-content-type

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入