How to transfer data to the Asp.net web interface via Windows Phone 7

I am wondering how you send data back and forth when using Windows Phone 7 and the asp.net web api?

I have this method in my webapi

public HttpResponseMessage Get(VerifyUserVm vm)
{
    if (ModelState.IsValid)
    {
        userService.ValidateUser(vm.Email);

        if (userService.ValidationDictionary.IsValid)
        {
            HttpResponseMessage reponse = Request.CreateResponse(HttpStatusCode.OK, userService.ValidationDictionary.ModelState["Success"]);
            return reponse;
        }
        else
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest, userService.ValidationDictionary.ModelState);
        }
    }

    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}

public class VerifyUserVm
{
    [Required]
    public string Email { get; set; }
}

      

and this code in my WP7

private void btnSignIn_Click(object sender, RoutedEventArgs e)
{
    string urlPath = String.Format(WebApiHelp.ApiUrl,"user","get");            
    UriBuilder uri = new UriBuilder(urlPath);
    uri.Query = "email=" + txtEmail.Text;
    webclient.OpenReadAsync(uri.Uri);
}

      

The url that is generated is like this: http://localhost:50570/api/user/get?email=c

but Vm is always null.

+3


source to share


1 answer


Web.Api dialog bindings of simple parameters (int, string, etc.) form the url and complex types (any other custom type) from the request body.

If you want to associate a complex type VerifyUserVm

with a url (for example: a query string) you need to annotate the parameter with FromUriAttribute

:

public HttpResponseMessage Get([FromUri]VerifyUserVm vm)
{
     //..
}

      

One more thing, the properties wrapper must match:



In your virtual machine, you have Email

uppercase E

, but you send Email

with lowercase E

.

So, you need to construct your parameter like this:

uri.Query = "Email=" + txtEmail.Text;

      

For Further Reading: How WebAPI Performs Parameter Binding

0


source







All Articles