mardi 18 août 2015

Set ASP.NET WEB API 2 custom route

I have created login form and want to check on server side if user with entered username exist or username and password match. I created angular service:

(function (app) {
    'use strict';

var baseApiPath = "http://localhost:55581/api/";

app.factory('loginService', ['$q', '$timeout', '$http',
    function ($q, $timeout, $http) {

        function getUser(username, password) {
            var deferred = $q.defer();
            $timeout(function () {
                $http({
                    method: 'GET',
                    url: baseApiPath + 'login/' + username + password,
                    data: JSON.stringify(username, password),
                    headers: { 'Content-Type': 'application/JSON' }
                }).success(function (data) {
                    deferred.resolve(data);
                }).error(function (error) {
                    console.log('data Error:', error);
                });
            }, 30);

            return deferred.promise;
        };

        return {
            getUser: getUser
        };
    }]);
})(angular.module('myApp'));

Custom Web Api controller is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using FoodOrdering.API.Models;

namespace FoodOrdering.API.Controllers
{
    public class LoginController : ApiController
    {
        private FoodOrderingContext db = new FoodOrderingContext();

        [Route("login/{username}/{password}")]
        public async Task<IHttpActionResult> GetUser(string username,string password)
        {
            User user = await db.Users.FindAsync(username, password);
            if (user == null)
            {
                return NotFound();
            }

            return Ok(user);
        }
    }
}

Please note that I've defined custom route. My question is: How can I get user from server if it exists and match with its password. Thanks!




Aucun commentaire:

Enregistrer un commentaire