简体   繁体   English

自定义API路由未公开POST方法

[英]Custom API routing not exposing POST method

I want to submit a form post via AJAX to my API. 我想通过AJAX向我的API提交表单。 I have already set up my WebApiConfig.cs class as follows to enable multiple GETs: 我已经按照以下步骤设置了WebApiConfig.cs类,以启用多个GET:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

config.Routes.MapHttpRoute(
    name: "ApiById",
     routeTemplate: "api/{controller}/{id}",
     defaults: new { id = RouteParameter.Optional },
     constraints: new { id = @"^[0-9]+$" }
);
config.Routes.MapHttpRoute(
    name: "ApiByName",
    routeTemplate: "api/{controller}/{action}/{name}",
    defaults: null,
    constraints: new { name = @"^[a-z]+$" }
);

config.Routes.MapHttpRoute(
    name: "ApiByAction",
    routeTemplate: "api/{controller}/{action}",
    defaults: new { action = "Get" }
);

Here is the controller action: 这是控制器动作:

[HttpPost]
[ActionName("ChangeStuff")]
public string ChangeStuff(int val1, int val2, int val3)
{
    //Code
}

And this is where I make the request: 这是我发出请求的地方:

var url = window.location.protocol + "//" + window.location.host + "/api" + "@Url.Action("ChangeStuff", "ApiControllerName")";
        $.ajax({
            url: url,
            type: "POST", //Works with GET
            data: { val1: val1, val2: val2, val3: val3 },
            contentType: "application/json:; charset=utf-8",
            dataType: "json",
            success: function (msg) {
            },
            failure: function (msg) {
            }
        });

However, I get this error back when making the post: 但是,发布此帖子时,我会收到此错误:

POST http://localhost:50627/api/ApiControllerName/ChangeStuff 404 (Not Found) 

I am thinking this is because of an issue with my API routing defaulting to GET requests and then failing to accept POST types (even though it is made explicit in the AJAX call). 我认为这是因为我的API路由存在问题,默认为GET请求,然后无法接受POST类型(即使在AJAX调用中已明确指定)。 It works just fine when making GET requests. 发出GET请求时,它工作得很好。

EDIT: Neither of the following attempts worked: 编辑:以下尝试均无效:

data: JSON.stringify( { currentobject } )
data: { data: { currentobject } }

Is it assured that the issue is not with the modified API routes? 是否可以确定问题不在于修改的API路由?

Try defining an object with the values you want to pass then telling your API Post action to expect that object instead of a list of individual values. 尝试使用要传递的值定义一个对象,然后告诉API Post操作期望该对象而不是单个值列表。

public class ApiPostModel
{
    public int Val1 { get; set; }
    public int Val2 { get; set; }
    public int Val3 { get; set; }
}

Then in your controller: 然后在您的控制器中:

[HttpPost]
[ActionName("ChangeStuff")]
public string ChangeStuff(ApiPostModel model)
{
    //Code
}

As long as the names of the keys you pass in your data{} block match up with the attribute names you should be good to go. 只要您在data{}块中传递的键名称与属性名称匹配,您就应该可以使用。

Either that or pass the values to the controller as querystrings. 要么将这些值作为查询字符串传递给控制器​​。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM