Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Xamarin.Forms with MVC Web API

$
0
0

Hello, I am having a hard time figuring out how to communicate back and forth between Xamarin.Forms and my Web API controller. I am able to hit my controller, but do not know how to use the response. Am I on the right track? I've had a hard time finding examples for controller back mobile project code. Here are the relevant snippets:

    //Project Mobile Side
    public void onLoginClicked (object sender, EventArgs e)
            {
                var url = BaseUrl + "/loginapi/login?username=" + eUserName.Text + "&password=" + ePassword.Text;

                var request = WebRequest.Create(url);
                request.BeginGetResponse(loginReturn, request);

            }

    //API Controller in MVC project
    public HttpResponseMessage Login(string userName, string password)
            {
                t_user user = new t_user();
                try
                {
                    var vUser = (from u in db.t_user
                                where u.email == userName && u.password == password
                                select new { u }).FirstOrDefault();

                    if (vUser == null)
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
                    }
                    FormsAuthentication.SetAuthCookie(userName, true);
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, vUser);
                    return response;
                    //return Json(vUser);
                }
                catch (Exception x)
                {
                    return null;
                }
            }

    //Project Mobile Side
    void loginReturn(IAsyncResult result)
            {
                var request = result.AsyncState as HttpWebRequest;
                HttpWebRequest test = (HttpWebRequest)result.AsyncState;

                try
                {
            //Tried multiple things here but get nothing I can use.
                    //var response = request.EndGetResponse(result);
                    //Stream test2 = response.GetResponseStream();
                    ////test2.Read();
                    //ad.RenderStream(response.GetResponseStream());
                }
                catch(Exception e)
                {
                    Debug.WriteLine(e);
                }
            }

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>