Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping value of a parameter in querystring to a DTO property

I am trying to find a way to get the value in this querystring to my DTO object.

example.org?code=abc

I have to map value of code to AuthorizationCode property (parameter names don't match either). I tried routing like this but it doesn't work.

[Route("?code={AuthorizationCode}", "GET")]
public class Registration
{
    public string AuthorizationCode { get; set; }
}

Since this is a callback URL, I don't have a chance to change it. How can I accomplish this?

like image 922
Ufuk Hacıoğulları Avatar asked Mar 17 '13 15:03

Ufuk Hacıoğulları


1 Answers

Read this earlier answer on ServiceStack's Routes. Routes should only contain the /path/info they should never contain the queryString which are automatically able to populate all Request DTOs by themselves.

If you just have a code property in your DTO like:

[Route("/registration", "GET")]
public class Registration
{
    public string Code { get; set; }
}

You can already populate it with: /registration?code=abc.

Otherwise if you want to insist having different names for queryString and DTO, you can try creating an Alias by annotating your DTOs as a [DataContract]:

[Route("/registration", "GET")]
[DataContract]
public class Registration
{
    [DataMember(Name="code")]
    public string AuthorizationCode { get; set; }
}

Either way you can always access the QueryString directly in your services or filters:

public MyService : Service 
{
    public object Post(Registration request) 
    {
        var code = base.Request.QueryString["code"];
    }
}
like image 153
mythz Avatar answered Oct 12 '22 20:10

mythz