【问题标题】:Handling multiple IDs in Route处理Route中的多个ID
【发布时间】:2019-02-15 13:42:22
【问题描述】:

据我了解,.NET Core MVC中的常规路由是[controller]/[action]/{id?}

但是,我正在尝试捕获以下 POST 请求,如下所示:

myDomain/MyController/MyAction/userID/anotherID/myInfo

我尝试了以下方法,但似乎不起作用:

public class MyController : Controller
{
    [HTTPPost]
    [Route("MyAction/{userID:minlength(2)}/{anotherID:int}/myInfo")]
    public IActionResult MyAction([FromRoute] string userID, [FromRoute] int anotherID, [FromBody] string stuffIWant)
    {
        return Ok();
    }
}

显然我没有正确处理路由,但我不确定如何从该路由获得userIDanotherID。我已将此操作发布到我的网站,并尝试使用相同的 URL 发布测试帖子,但没有得到响应。

【问题讨论】:

    标签: c# routing asp.net-core-mvc


    【解决方案1】:

    改为:

    public class MyController : Controller
    {
        [HTTPPost]
        [Route("MyAction/{userID:minlength(2)}/{anotherID:int}/myInfo")]
        public IActionResult MyAction(string userID, int anotherID, [FromBody]  string stuffIWant)
        {
            return Ok();
        }
    }
    

    【讨论】:

    • 格式化:)。检查答案
    猜你喜欢
    • 2011-06-03
    • 2016-02-29
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多