C#web-api post有两个参数

时间:2014-12-16 08:20:09

标签: c# jquery ajax asp.net-web-api

我试图将两个参数发布到以下函数,但我没有设法达到该函数:

public void SetShopSubCategories([FromBody]string userId, int []subCategories )
{

}

这就是我的发布方式:

var subCategories = [ 1, 2, 3, 4, 5];
var userId = "123";

            $.ajax({
                type: "POST",
                url: "/Category/SetShopSubCategories/",
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify(userId, subCategories),
                success: function () {
                    alert("OK");
                },
                error: function () {
                    alert("error");
                }

当我只发布一个参数时,它会很顺利,我可以达到这个功能:

public void SetShopSubCategories([FromBody]string userId )
{

}

var userId = "123";

        $.ajax({
            type: "POST",
            url: "/Category/SetShopSubCategories/",
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify(userId, subCategories),
            success: function () {
                alert("OK");
            },
            error: function () {
                alert("error");
            }

这个也很顺利:

public void SetShopSubCategories( int []subCategories )
{

}

var subCategories = [ 1, 2, 3, 4, 5]; 

            $.ajax({
                type: "POST",
                url: "/Category/SetShopSubCategories/",
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify(subCategories),
                success: function () {
                    alert("OK");
                },
                error: function () {
                    alert("error");
                }

我的RoutConfig:

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            GlobalConfiguration.Configuration.Routes.MapHttpRoute(
                name: "SetCategories",
                routeTemplate: "{controller}/{action}"
            );


            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

3 个答案:

答案 0 :(得分:3)

模型

public class Mymodel
    {
        public string UserId { get; set; }        
        public int[] subCategories { get; set; }
    }

控制器操作

[HttpPost]
        public void SetShopSubCategories([FromBody]Mymodel model)
        {

        }

Ajax Call:

var subCategories = [1, 2, 3, 4, 5];
var userId = "123"
$.ajax({
    type: "POST",
    url: "/api/Values",
    contentType: "application/json; charset=utf-8",  
    data: JSON.stringify({ userid: userId, subCategories: subCategories }),
    success: function () {
        alert("OK");
    },
    error: function () {
        alert("error");
    }
});

以下是链接:http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

由于流的类型,您会发现多个参数不允许或有问题。

答案 1 :(得分:0)

尝试以下代码并添加dataType: 'json'

 $.ajax({
            type: "POST",
            url: "/Category/SetShopSubCategories",
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ userId  : userId, subCategories : subCategories}),
            dataType: 'json',
            success: function () {
                alert("OK");
            },
            error: function () {
                alert("error");
            }

答案 2 :(得分:0)

更改您的路线配置以接受两个参数:更新或添加具有不同名称的新路线

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}/{category}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, category= UrlParameter.Optional}
            );