【问题标题】:C# web-api post to function with two parametersC# web-api post 使用两个参数运行
【发布时间】:2015-02-14 12:10:21
【问题描述】:

我正在尝试将两个参数发布到以下函数,但我无法访问该函数:

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

【问题讨论】:

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


    【解决方案1】:

    型号

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

    控制器动作

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

    Ajax 调用:

    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

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

    【讨论】:

    • 是的,这就是我所做的。
    • 如果你必须支持多个而不是你必须构建从自定义 FromBody 到它如何绑定的许多东西,甚至你必须存储你的流是自定义缓冲区等等。
    【解决方案2】:

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

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

    【讨论】:

    • 仍然无法达到功能。
    【解决方案3】:

    尝试下面的代码并添加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");
                }
    

    【讨论】:

    • 还是无法达到功能。
    • 你为什么把[FromBody]放在这里? @Eyal
    猜你喜欢
    • 2017-08-03
    • 1970-01-01
    • 2021-05-06
    • 2017-08-09
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多