0

I am using web api and creating a login functionality.(n tier architecture). I am getting a null response in Login Controller.

First I want to confirm that is my code logic is correct and if it is correct then why i am getting response null in

 HttpResponseMessage response = client.GetAsync("api/Login/Login").Result;

My Project UI Login Controller Code LoginCOntroller.cs

   [HttpPost]
        public ActionResult Login(LoginViewModel loginViewModel)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:63465/");

            HttpResponseMessage response = client.GetAsync("api/Login/Login").Result;
            if (response.IsSuccessStatusCode)
            {
                return RedirectToActionPermanent("Index", "Project");
            }
            return View(loginViewModel);
        }

My Api Login Controller Code LoginController.cs

public HttpResponseMessage Login(LoginViewModel loginViewModel)
{
    if (ModelState.IsValid)
    {
        UserEntity userEntity = new UserEntity();
        userEntity.Email = loginViewModel.UserName;
        userEntity.Password = loginViewModel.Password;

        var login = new LoginManager().LoginAuthentication(userEntity);

        if (login != null)
        {
            return Request.CreateResponse(login);
        }
    }

    return Request.CreateResponse(true);
}

My Login manager Class LoginManager.cs

  public class LoginManager
    {

    public UserEntity LoginAuthentication(UserEntity userDetails)
    {
        var userDetail = new LoginDa().LoginAuthentication(userDetails);
        return userDetail;
    }
}

My Data access layer LoginDa.cs

public class LoginDa
{
    public UserEntity LoginAuthentication(UserEntity login)
    {
        using (var context = new ArcomDbContext())
        {
            var loginDetail = context.userInformation.FirstOrDefault(p => p.Email == login.Email && p.Password == login.Password);
            return loginDetail;
        }
    }
}
7
  • can you post your routing code? Commented May 19, 2016 at 7:13
  • This is my Route.config ` public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }` Commented May 19, 2016 at 7:21
  • have you added a [Httpget] annotation above LoginController? Commented May 19, 2016 at 7:24
  • @KasperDue [Httpget] annotation for Api LoginController Commented May 19, 2016 at 7:26
  • i mean a [HttpGet] annotation above your Login action inside your API LoginController. Commented May 19, 2016 at 7:27

1 Answer 1

1

GetAsync method will only call GET methods on your API, so if you have prefixed your method in API with [HttpPost] (which it should be) you need to call POST methods of HttpClient class (For example - PostAsJsonAsync).

Secondly you're not following the async/await pattern? You're calling an async method, but not awaiting it.

More on that here http://blog.stephencleary.com/2012/02/async-and-await.html .

With all that said below is how your code should look

public async Task<ActionResult> Login(LoginViewModel loginViewModel)
{
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("http://localhost:63465");
    var response = await client.PostAsJsonAsync("api/login", loginViewModel); //Since it's a post it will automatically trigger corresponding post method on your webAPI
    if(response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        return RedirectToActionPermanent("Index", "Project");
    }
    return View(loginViewModel);
}

Also are you able to call you API using fiddler/Postman ?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.