First we create Azure SQL Server Database. Go to Azure Portal.
Create Azure SQL server database
Step 1
Create SQL Database and Database server. Go to portal bottom + DATA SERVICES->SQL DATABASE->QUICK CREATE then give your database name, server name, and password. After that click create SQL Database.
![]()
Step 2
Next open SQL Database Design View, then Create New Table and name for Login.
Step 3
Then Create Login Table Rows. Here create three rows: ID, Username, Password. After creating rows go to Visual studio and create New WEB API.
WEB API
Step 4
Open Visual Studio->New Project->Templates->Visual C#->Web ->ASP.NET Web Application, then open new pop up window and select WEB API. Then give Project Name and Project Location.
Step 5
Next go to Solution Explorer-> Project Name-> Models then Right Click to Add -> ADO.NET Entity Data Model then open new Dialog box then Give Item Name and follow the given steps.
Step 6
Select EF Designer from Database.
Step 7
Select New Connection.
Step 8
Here give Server name, Username and password then finally select database.
Step 9
Step 10
Step 11
Next go to Solution Explorer-> Project Name-> App Start -> WebApiConfig.cs. open cs code then give below code at the end of register method. This code will be converted from XML format to JSON format in output.
- var json = config.Formatters.JsonFormatter;
-
- json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
-
- json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
-
- config.Formatters.Remove(config.Formatters.XmlFormatter);
Step 12
Next go to Solution Explorer-> Project Name-> Controllers then Right Click to Add->Controllers then Open New Popup window you can choose Web API 2 Controller with read/write actions after give controller name.
Step 13
Then Open once you have created ControllerName.cs. Here you create two methods, Xamarin_reg and Xamarin_login.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using XamarinLogin.Models;
- namespace XamarinLogin.Controllers
- {
- public class LoginController: ApiController
- {
- xamarinloginEntities db = new xamarinloginEntities();
- /
- [HttpPost]
- [ActionName("XAMARIN_REG")]
-
- public HttpResponseMessage Xamarin_reg(string username, string password)
- {
- Login login = new Login();
- login.Username = username;
- login.Password = password;
- db.Logins.Add(login);
- db.SaveChanges();
- return Request.CreateResponse(HttpStatusCode.Accepted, "Successfully Created");
- }
- [HttpGet]
- [ActionName("XAMARIN_Login")]
-
- public HttpResponseMessage Xamarin_login(string username, string password)
- {
- var user = db.Logins.Where(x => x.Username == username && x.Password == password).FirstOrDefault();
- if (user == null)
- {
- return Request.CreateResponse(HttpStatusCode.Unauthorized, "Please Enter valid UserName and Password");
- }
- else
- {
- return Request.CreateResponse(HttpStatusCode.Accepted, "Success");
- }
- }
- }
- }
![]()
Next steps -- How to Publish WEB API in Azure Portal.
Step 14
Next go to Solution Explorer-> Project Name then Right Click to Click Publish then open new popup window for Publish Web.
Step 15
Step 16
Step 17
Step 18
Step 19
![]()
Finally, we have successfully created and Hosted WEB API Using Azure SQL Server database.