locked
How to create a Web Api REST web service for User registration for Mobile client to consume RRS feed

  • Question

  • User-1980778524 posted

    Hi newbie here. I have been searching for sample code to learn how to create Asp.net Web Api for user registrtion.  I have learnt the Asp.net MVC and have understanding on the Asp.net Web Api on Routing and other stuff.

    I have seen sample using Asp.net MVC app to register use thru asp.net form. But my requirement is providing a Web api or REST web service to mobile User to consume. On Mobile Client I will provide Username, Email and others for registration.

    Can you help me to provide me a reference or link to read or sample app.  I need web api user registration for iOS/Android to consume.

    On the server side , how to handle the data from mobile and insert into SQL server database?

    Thanks

    Thursday, April 20, 2017 10:35 AM

Answers

  • User-2057865890 posted

    Hi SkyDriving,

    problems:

    A) when having these two methods on,

        public tblUser Get(int Id){}
        public HttpResponseMessage Get(String strusername){}

    a1) when enter name:  user1 at : http://localhost:xxxx/api/Users/user1

    The result is : httpCode 404 : bad request

    a2) When enter userId: 1 at : http://localhost:xxxx/api/Users/1

    The result is OK, I got user1 data.

    a3) When enter userId : 3

    I got null.   It should say no such user!

    B) when having only below method on,

           public HttpResponseMessage Get(String strusername){}

    When enter name : User1 at : http://localhost:xxxx/api/Users/user1

    The result:  I have two users in Json !!! user1 and user2.  Why?

    You have a Web API controller with multiple get methods and have trouble with mapping the routes. When you create a Web API project and find that some requests don't get routed the way you expect, this article will help.

    Adding Route Attributes

    [Route("api/users/GetById/{Id}")]
    public tblUser Get(int Id)
    
    [Route("api/users/GetByUserName/{strusername)}")]
    public HttpResponseMessage Get(String strusername)

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 24, 2017 2:44 AM

All replies

  • User475983607 posted

    On the server side , how to handle the data from mobile and insert into SQL server database?

    See the learn link above.  There is are sections on on mobile clients, security, and data.

    https://www.asp.net/web-api/overview/mobile-clients

    Thursday, April 20, 2017 11:04 AM
  • User-1980778524 posted

    Hi mgebhard,

    Thanks for the link. I have read it and understand using HttpClient in Client to post the data.

    My problem now is to learn to code the Web Api for user registration which consume by mobile client.

    in the Client , I use string strURL ="Http://domainname.com/api/Register

    I need some sample to learn or some basic code to start for web api

    Thanks

    Thursday, April 20, 2017 12:12 PM
  • User475983607 posted

    Hi mgebhard,

    Thanks for the link. I have read it and understand using HttpClient in Client to post the data.

    My problem now is to learn to code the Web Api for user registration which consume by mobile client.

    in the Client , I use string strURL ="Http://domainname.com/api/Register

    Thanks

    It works the same way. The only difference are you use the HTTP action; GET, POST, PUT, etc...  Rather than using POST for everything.

    I imagine you have registration requirements of some sort that define the what registration is in terms of DB schema and models?  From there you would create a Registration API controller and flush out the GET, POST, PUT, and DELETE methods, bascially the CRUD.  These CRUD method would call into your business and data access layers to retrieve and save data.

    The tutorials in the previous post explain how to do this. Maybe the issue is you don't know how to do an HTTP request in the mobile app?

    I need some sample to learn or some basic code to start for web api

    What programming framework are you using to build the mobile app?  

    Thursday, April 20, 2017 1:39 PM
  • User-1980778524 posted

    Currently, I am working on Xcode/iOS and Xamarin Android for mobile client.  I dont have much experience on Asp.net Web api. I propose to use this.

    I am a bit confuse on the server-side for SQL server handling.

    1) ON Asp.net MVC(App), there are approaches like : 1) Using EntityFramework 2) Not using EF to handle the CRUD on SQL Server.

    For this, I can use Form to sign up user and using Controller and View to design UI. 

    Since I am not using Asp.netMVC, I use asp.net Web Api to create REST service for mobile client to signUp and do other transaction.

    2) How I set Up the SQL Connection for  CRUD in Web Api ?  Do I need EF ? How to do it with or without EF ? Which is better approach?

    I need to start from the basic first.

    Your help is much appreciated. 

    Thanks

    Friday, April 21, 2017 2:50 AM
  • User-2057865890 posted

    Hi SkyDriving,

    1) ON Asp.net MVC(App), there are approaches like : 1) Using EntityFramework 2) Not using EF to handle the CRUD on SQL Server.

    For this, I can use Form to sign up user and using Controller and View to design UI. 

    Since I am not using Asp.netMVC, I use asp.net Web Api to create REST service for mobile client to signUp and do other transaction.

    2) How I set Up the SQL Connection for  CRUD in Web Api ?  Do I need EF ? How to do it with or without EF ? Which is better approach?

    I need to start from the basic first.

    You could program CRUD (create, read, update, delete) operations with Entity Framework in Web API. ASP.NET Web API  makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices

    Working with Data in ASP.NET Web API

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/

    Best Regards,

    Chris

    Friday, April 21, 2017 8:44 AM
  • User-1980778524 posted

    Hi Chris

    I read the link you provided. .

    In that link , I saw the Data Model below. I think that is for asp.net MVC. The form post back will use ModelState and attributes

    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    namespace BookService.Models
    {
        public class Author
        {
            public int Id { get; set; }
            [Required]
            public string Name { get; set; }
        }
    }

    I have some Questions

    1) I am really need a good web api sample for mobile client for reference.  I am not sure IF I can use Attribute like [required] in creating REST api.

        I am new to this.  I see this in Asp.net MVC a lot as it uses Postback Form to consume the service. Now, I need to create REST api for mobile client which uses HttpClient to consume and not using Form.

    2) if I dont add Ado.net Entities DataModel ,How to set up SQL Server connection and use Ado.net and Classes to do CRUD? if this doable?

    Thanks

    Friday, April 21, 2017 2:16 PM
  • User475983607 posted

    1) I am really need a good web api sample for mobile client for reference.  I am not sure IF I can use Attribute like [required] in creating REST api.

    Learning is a personal journey.  There is nothing we can do on the forum except point you to reference material. It is up to you to do the learning.  

    The [required] attribute is related to the model and yes it can be used in Web API.  

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api

    I am new to this.  I see this in Asp.net MVC a lot as it uses Postback Form to consume the service. Now, I need to create REST api for mobile client which uses HttpClient to consume and not using Form.

    While the two frameworks look similar, Web API is not MVC don't try to comparing the two.  You might want to start with understand what a RESTful service is before jumping into Web API.

    https://en.wikipedia.org/wiki/Representational_state_transfer

    Getting started with Web API

    https://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

    Keep in mind, you'll need to consult Xcode/iOS and Xamarin Android documentation for HTTP REST syntax and examples.

    2) if I dont add Ado.net Entities DataModel ,How to set up SQL Server connection and use Ado.net and Classes to do CRUD? if this doable?

    Of course ADO.NET and Entity Framework implement CRUD.  You'll need to spend time going through ADO.NET and/or entity framework tutorials to get a better understanding of the technology.

    Again, visit the learn link above.

    https://www.asp.net/web-api/overview/data

    Friday, April 21, 2017 3:10 PM
  • User-1980778524 posted

    Hi all,

    Finally, I created a web Api. but I have some problems.

    1)in SQL Server DB,I have data in tblUsers

     UserId   UserName  UserPassword  UserEmail
      1        user1     123456        usr1@wow.com
      2        user2     678902        usr2@woo.com

    2) have set up the Ado.net Entities DataModel and Class Library for this project

           BookingEntities

    problems:

    A) when having these two methods on,

        public tblUser Get(int Id){}
        public HttpResponseMessage Get(String strusername){}

    a1) when enter name:  user1 at : http://localhost:xxxx/api/Users/user1

    The result is : httpCode 404 : bad request

    a2) When enter userId: 1 at : http://localhost:xxxx/api/Users/1

    The result is OK, I got user1 data.

    a3) When enter userId : 3

    I got null.   It should say no such user!

    B) when having only below method on,

           public HttpResponseMessage Get(String strusername){}

    When enter name : User1 at : http://localhost:xxxx/api/Users/user1

    The result:  I have two users in Json !!! user1 and user2.  Why?

    Thanks


    ------------ here the UsersController :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using BookingDataAccess;

    namespace MyBookingHub.Controllers
    {
        public class UsersController : ApiController
        {
            public IEnumerable<tblUser> Get()
            {
                using (BookingEntities entities = new BookingEntities())
                {
                    return entities.tblUsers.ToList();
                }
            }

          
            public tblUser Get(int Id)
            {
                using (BookingEntities entities = new BookingEntities())
                {
                    return entities.tblUsers.FirstOrDefault(e => e.UserId == Id);
                }
            }

            public HttpResponseMessage Get(String strusername)
            {
                using (BookingEntities entities = new BookingEntities())
                {
                    var entity = entities.tblUsers.Where(x => x.UserName == strusername);
                    //var entity = entities.tblUsers.FirstOrDefault(e => e.UserName == strusername);
           
                   if (entity != null)
                    {
                        return Request.CreateResponse(HttpStatusCode.OK, entity);
                    }
                    else
                    {
                        return Request.CreateErrorResponse(HttpStatusCode.NotFound, " Can register : " + strusername);
                    }
                }

            }
        }
    }

    Saturday, April 22, 2017 4:04 PM
  • User-2057865890 posted

    Hi SkyDriving,

    problems:

    A) when having these two methods on,

        public tblUser Get(int Id){}
        public HttpResponseMessage Get(String strusername){}

    a1) when enter name:  user1 at : http://localhost:xxxx/api/Users/user1

    The result is : httpCode 404 : bad request

    a2) When enter userId: 1 at : http://localhost:xxxx/api/Users/1

    The result is OK, I got user1 data.

    a3) When enter userId : 3

    I got null.   It should say no such user!

    B) when having only below method on,

           public HttpResponseMessage Get(String strusername){}

    When enter name : User1 at : http://localhost:xxxx/api/Users/user1

    The result:  I have two users in Json !!! user1 and user2.  Why?

    You have a Web API controller with multiple get methods and have trouble with mapping the routes. When you create a Web API project and find that some requests don't get routed the way you expect, this article will help.

    Adding Route Attributes

    [Route("api/users/GetById/{Id}")]
    public tblUser Get(int Id)
    
    [Route("api/users/GetByUserName/{strusername)}")]
    public HttpResponseMessage Get(String strusername)

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 24, 2017 2:44 AM