WebAPI 500内部服务器错误,未调用api方法

时间:2018-11-11 21:08:22

标签: c# wpf asp.net-web-api

因此,我正在制作一些基于WebAPI的应用程序,并且在调用API方法进行登录时遇到问题。这是服务器端和客户端这两种方法的代码。 服务器端:

[HttpPost]
[ActionName("Login")]
public IHttpActionResult Login(LoginVM model)
{
    if (!ModelState.IsValid)
    {
        return BadRequest("bad-data");
    }

    bool isValid = false;
    int roleId = 0;

    using (var db = new FoodOrderingContext())
    {
        if (db.Users.Any(x => x.UserName.Equals(model.Username) && x.Password.Equals(model.Password)))
        {
            isValid = true;
            roleId = db.Users.First(x => x.UserName.Equals(model.Username)).RoleId;
        }
    }

    if (!isValid)
    {
        return NotFound();
    }

    return Ok(roleId.ToString());
}

客户端:

public int Login(string usr, string pass)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://localhost:52407/");
        LoginVM model = new LoginVM(usr, pass);
        var postTask = client.PostAsJsonAsync<LoginVM>("api/account/Login", model);
        postTask.Wait();

        var result = postTask.Result;
        if (result.IsSuccessStatusCode)
        {
            MessageBox.Show("Success!");
        }
        else
        {
            MessageBox.Show(result.StatusCode.ToString());
        }
    }

    return 0;
}

我尝试设置断点,但无济于事。代码根本不会进入服务器端方法。有任何想法吗?我在做什么错了?

0 个答案:

没有答案
相关问题