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

20,180

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.

Share:
20,180

Related videos on Youtube

Luke Puplett
Author by

Luke Puplett

Started out as a Windows PSS engineer for Microsoft UK, then built industrial scale automation solutions for banks before turning to application and systems programming on .NET and Azure.

Updated on February 23, 2021

Comments

  • Luke Puplett
    Luke Puplett about 3 years

    In WebApi I can decorate a parameter on a controller action with [FromUri] to have the components of the URI 'deserialized', if you will, into a POCO model; aka model-binding.

    Despite using MVC since 2.0, I've never used it for websites (I don't know why). What's the equivalent of it in ASP.NET MVC 5?

    The attribute doesn't seem to be recognised in the IDE unless I need to reference a library.

    I'd like ~/thing/2014/9 to bind to the model below:

    public class WhateverModel
    {
        public int Year { get; set; }
        public int Month { get; set; }
    }
    

    Thanks

    Update

    In the other question (link above), the OP says:

    However, switch this to plain MVC not WebApi and the default model binder breaks down and cannot bind the properties on objects in the nested array

    Which implies that he's using the attribute from the WebApi. I guess. I don't have those references, because I'm in MVC, so is (ab)using WebApi's version the accepted way to do this in MVC?

    Update 2

    In the answer to that question is:

    You need to construct your query string respecting MVC model binder naming conventions.

    Additionally [FromUri] attribute in your example action is completely ignored, since it's not known to MVC DefaultModelBinder

    So I'm still left not knowing what to do or what on Earth the OP was even talking about in that question, if he was getting some success with the wrong attribute.

    I guess I'm hoping for a clear answer and not the mud of that other question.

    • Sam
      Sam about 9 years
    • Luke Puplett
      Luke Puplett about 9 years
      Hi Sam, see my update in the question.
    • Sam
      Sam about 9 years
      Sorry, I guess I didn't understand the question. As long as all of the properties in your model are primitive types or string, you shouldn't need to do anything extra to bind it through the URI query string using MVC's built in model binding. CodeCaster's answer should work fine.
    • Luke Puplett
      Luke Puplett about 9 years
      Thanks Sam, no worries about the dupe, it was legitimate but I guess I wasn't able to connect the dots from it. So in the other question, the FromUri was a red-herring, it was "just working".
  • Luke Puplett
    Luke Puplett about 9 years
    I just came back on here to answer my own question, that it'll just work :-) I just stuck a model in the signature and it just did it. I was already using attribute routing, you. Anyway, perfect. Thanks. I always assumed model binding was for payloads in the body.