如何在路由配置中不使用“?id =”从URL获取ID?

时间:2017-09-18 11:51:13

标签: c# asp.net-mvc url routes config

有很多问题,但没有一个澄清这个具体情况。

我想创建一个与控制器或操作名称无关的URL,我希望将ID值直接放在那里(而不是CX)。

含义我想要以下网址:

CX[Int]

使用id“43”调用控制器“nevermind”中的“what”动作。

在路线配置中我有:

"?id="

如果我输入网址http://localhost/word/43 ,它可以正常工作,如果我输入我想要的网址routes.MapRoute( name: "yes", url: "word/{id}", defaults: new { controller = "nevermind", action = "whatever"}, constraints: new { id = @"\d+" } ); ,它不起作用,则ID会显示为空。

控制器方法是

localhost/word?id=43

1 个答案:

答案 0 :(得分:1)

您可以使用属性

[Route("api/[controller]")]
public class YourController : Controller
{
    HttpPost("{id}")
    public IActionResult YorMethod(int id)
    {

    }
}

然后在你的请求中: http://localhost:5166/api/YourController/YorMethod/2

相关问题