I have create a WEB API
using ASP.NET MVC WEB API 2.0
. Web API contain methods like login
, register
, etc.
I am using AngularJS
to call the WEB API
methods, But it call twice. For e.g. when I call POST
method it first call OPTION
(I have not created any OPTION
method) and then it call the POST
method.
My Code: (for login)
HTML:
<div class="container" ng-controller="UserAccount">
<form ng-submit="loginNow(user)">
<input type="email" placeholder="Enter Email" required ng-model="user.email" id="txtEmail" class="form-control" />
<br />
<input type="password" placeholder="Enter Password" required ng-model="user.password" id="txtPwd" class="form-control" />
<br />
<button type="submit" class="btn btn-primary">Login</button>
</form>
</div>
AngularJS Controller:
$scope.loginNow = function (user) {
$http.post(rootURL + "login", { email: user.email, password: user.password })
.success(function (data) {
if (data.id > 0) {
sessionStorage.setItem('userid', data.id);
window.location = '/';
}
});
}
WEB API:
public class UsersController : ApiController
{
private UserEntities db = new UserEntities();
// GET: api/Users
public IQueryable<Users_tbl> GetUsers_tbl()
{
return db.Users_tbl;
}
[ActionName("login")]
public IHttpActionResult PostUser_Login(string email, string password)
{
int id = db.Users_tbl.Where(a => a.email == email && a.password == password).Select(a => a.id).SingleOrDefault();
if(id <= 0)
{
return NotFound();
}
return Ok(id);
}
[ActionName("register")]
public int PostUser_Register(string email, string password)
{
int id = db.Users_tbl.Where(a => a.email == email).Select(a => a.id).SingleOrDefault();
if(id > 0)
{
return 1;
}
Users_tbl user = new Users_tbl();
user.email = email;
user.password = password;
try
{
db.Users_tbl.Add(user);
db.SaveChanges();
}
catch
{
return 2;
}
return 0;
}
// GET: api/Users/5
[ResponseType(typeof(Users_tbl))]
public IHttpActionResult GetUsers_tbl(int id)
{
Users_tbl users_tbl = db.Users_tbl.Find(id);
if (users_tbl == null)
{
return NotFound();
}
return Ok(users_tbl);
}
// PUT: api/Users/5
[ResponseType(typeof(void))]
public IHttpActionResult PutUsers_tbl(int id, Users_tbl users_tbl)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != users_tbl.id)
{
return BadRequest();
}
db.Entry(users_tbl).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!Users_tblExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
[ActionName("profile")]
// POST: api/Users
[ResponseType(typeof(Users_tbl))]
public IHttpActionResult PostUsers_tbl(Users_tbl users_tbl)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Users_tbl.Add(users_tbl);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = users_tbl.id }, users_tbl);
}
// DELETE: api/Users/5
[ResponseType(typeof(Users_tbl))]
public IHttpActionResult DeleteUsers_tbl(int id)
{
Users_tbl users_tbl = db.Users_tbl.Find(id);
if (users_tbl == null)
{
return NotFound();
}
db.Users_tbl.Remove(users_tbl);
db.SaveChanges();
return Ok(users_tbl);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool Users_tblExists(int id)
{
return db.Users_tbl.Count(e => e.id == id) > 0;
}
}