Model not required for RedirectToRoute

I've searched up and down for an answer to what I thought would be a common problem, but couldn't 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 "populate" 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 pass it to another controller like this:

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

      

PROBLEM: myUser passes as null. None of the properties of the model are linked.

Here's what I made sure:

  • My routing is good because the breakpoint in SsoRedirect actually hits.
  • When I do a trace in the fiddler, I can see all the relevant query string parameters.
    1. I understand that I cannot pass complex objects with Enums and other complex data types, so everything is limited to strings.

So ... For life, I can't figure out why the model is optional and hopes for some help here.

+3


source to share


1 answer


If you are using a complex type as a parameter for your web API controller and receiving it via GET, you need to tell the controller that it can create an object from the request parameters sent in the request. Add an attribute [FromUri]

and your object should be instantiated fine:



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

      

+1


source







All Articles