具有自定义操作名称的 MVC Web API Post 方法

MVC Web API Post method with custom action name

我还为每个进程编写了自定义操作名称。

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

在 webapi 配置中我是这样写的,在控制器中,我是这样开始的

[System.Web.Http.HttpPost]
public HttpResponseMessage Control(List<string>codes,string updateperson,string refPeriod, int actualyear, int actualmonth)
{

对于 get 方法,一切正常,但对于 post 方法,它不起作用并给出如下错误。

正文中post我发

{codes: ["CPM2018-004"], updateperson: "E3852", refPeriod: "SFC18", actualyear: 2018, actualmonth: 12}

Request URL:http://localhost:50941/Api/Investment/Control Request Method:POST Status Code:404 Not Found Remote Address:[::1]:50941 Referrer Policy:no-referrer-when-downgrade

如何使用自定义操作名称接收 post 对网络 API 的请求?

创建模型以保存发布的值。

public class ControlViewModel {
    public List<string> codes { get; set; }
    public string updateperson { get; set; }
    public string refPeriod { get; set; }
    public int actualyear { get; set; }
    public int actualmonth { get; set; }
}

然后更新操作以期望请求的 BODY 中的数据

public class InvestmentController : ApiController {

    [HttpPost]
    public HttpResponseMessage Control([FromBody]ControlViewModel data) {
        //...
    }

    //...
}

引用Parameter Binding in ASP.NET Web API