What's the WebApi [FromUri] equivalent in ASP.NET MVC?

笑着哭i 提交于 2019-12-04 23:59:28

It'll Just Work™:

[HttpGet]
public ActionResult Thing(WhateverModel model)
{
    // use model
    return View();
}

At least, when using the URL /thing?Year=2014&Month=9.

The problem is your routing. The URL /thing/2014/9 won't map using MVC's default route, as that is /{controller}/{action}/{id}, where {id} is an optional int.

The easiest would be to use attribute routing:

[HttpGet]
[Route("/thing/{Year}/{Month}"]
public ActionResult Thing(WhateverModel model)
{
    // use model
    return View();
}

This will map the URL to your model.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!