3

I've scoured up and down for the answer to what I thought would be a common problem, but haven't been able to find a solution... so here's the question:

I have the following model:

public class UserModel
    {
        public string userType { get; set; }
        public string userName { get; set; }
        public string passWord { get; set; }
        public string userClaim { get; set; }
        public string redirectAction { get; set; }
        public string jsonWebToken { get; set; }
        public string message { get; set; }

    }

I'm "populating" this model in a Web API controller:

return RedirectToRoute("Default", new
            {
                controller = "Redirect",
                action = "SsoRedirect",
                method = "Get",
                userType = thisUser.userType,
                userName = thisUser.userName,
                passWord = thisUser.passWord,
                userClaim = thisUser.userClaim,
                redirectAction = thisUser.redirectAction,
                jsonWebToken = thisUser.jsonWebToken,
                message = thisUser.message,

            });

Then passing it to another controller like so:

[HttpGet]
[Route("SsoRedirect")]     
public IHttpActionResult SsoRedirect(UserModel myUser)
{
    ....
}

THE PROBLEM: myUser is coming through as null. None of the properties on the model are being bound.

Here's what I've made sure of:

  1. My routing is good, because a breakpoint in the SsoRedirect is in fact being hit.
  2. When I do a trace in fiddler I can see all the appropriate query string params being passed.
    1. I understand that I can't pass complex objects with Enums and other complex data types so everything is limited to strings.

So... For the life of me I can't figure out why the model isn't binding and hoping for some help here.

kickinchicken
  • 1,281
  • 4
  • 18
  • 40
  • possible duplicate of [ASP.NET MVC Web Api Get Not Mapping QueryString To Strongly Typed Parameter](http://stackoverflow.com/questions/12589988/asp-net-mvc-web-api-get-not-mapping-querystring-to-strongly-typed-parameter) – Evan Mulawski Jul 22 '15 at 15:06
  • I don't think it's a duplicate because I'm only using strings and those should be available for model binding according to the article listed in your link.. – kickinchicken Jul 22 '15 at 15:19
  • 1
    No, you're using a complex type, `UserModel` - you need to prepend `[FromUri]` – levelnis Jul 22 '15 at 15:50
  • Thank you thank you thank you!!!! The From URI attribute totally did the trick. @levelnis: Please post an answer and I will mark it as so. – kickinchicken Jul 23 '15 at 13:41

1 Answers1

1

If you're taking a complex type in as a parameter to your Web API controller and accessing it via GET, you need to tell the controller that it can create the object from the querystring parameters sent across in the request. Add the [FromUri] attribute and your object should be created fine:

[HttpGet]
[Route("SsoRedirect")]     
public IHttpActionResult SsoRedirect([FromUri]UserModel myUser)
{
    ....
}
levelnis
  • 7,665
  • 6
  • 37
  • 61