WebAPI 500 internal server error, not calling api method










0














So, I'm making some WebAPI based application and I have a problem when calling API method for login. Here are the code of both of the methods, server and client side.
Server side:



[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());



Client side:



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;



I tried making breakpoints, but to no avail. Code does not enter the server side method at all. Any ideas? What am I doing wrong?










share|improve this question























  • Are you sure the route is ../api/account/Login and not ../api/Login?
    – Crowcoder
    Nov 11 '18 at 21:14










  • @Crowcoder Yes, I'm sure. I tried it with api/login, but I got "not found" error as the result. Again, api method was not called.
    – Alex
    Nov 11 '18 at 21:16










  • Turn on break on all CLR exceptions and debug to see where it is throwing an error.
    – Crowcoder
    Nov 11 '18 at 21:18










  • @Crowcoder The problem is the application does not break at all, I'm just not being able to login.
    – Alex
    Nov 11 '18 at 21:28










  • If you are getting a 500 error it will break if you turn it on in the exception settings. This is a way to see where an exception is thrown if it is otherwise swallowed. I'm betting it will break somewhere in the pipeline before it even gets to your controller.
    – Crowcoder
    Nov 11 '18 at 21:30















0














So, I'm making some WebAPI based application and I have a problem when calling API method for login. Here are the code of both of the methods, server and client side.
Server side:



[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());



Client side:



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;



I tried making breakpoints, but to no avail. Code does not enter the server side method at all. Any ideas? What am I doing wrong?










share|improve this question























  • Are you sure the route is ../api/account/Login and not ../api/Login?
    – Crowcoder
    Nov 11 '18 at 21:14










  • @Crowcoder Yes, I'm sure. I tried it with api/login, but I got "not found" error as the result. Again, api method was not called.
    – Alex
    Nov 11 '18 at 21:16










  • Turn on break on all CLR exceptions and debug to see where it is throwing an error.
    – Crowcoder
    Nov 11 '18 at 21:18










  • @Crowcoder The problem is the application does not break at all, I'm just not being able to login.
    – Alex
    Nov 11 '18 at 21:28










  • If you are getting a 500 error it will break if you turn it on in the exception settings. This is a way to see where an exception is thrown if it is otherwise swallowed. I'm betting it will break somewhere in the pipeline before it even gets to your controller.
    – Crowcoder
    Nov 11 '18 at 21:30













0












0








0


0





So, I'm making some WebAPI based application and I have a problem when calling API method for login. Here are the code of both of the methods, server and client side.
Server side:



[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());



Client side:



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;



I tried making breakpoints, but to no avail. Code does not enter the server side method at all. Any ideas? What am I doing wrong?










share|improve this question















So, I'm making some WebAPI based application and I have a problem when calling API method for login. Here are the code of both of the methods, server and client side.
Server side:



[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());



Client side:



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;



I tried making breakpoints, but to no avail. Code does not enter the server side method at all. Any ideas? What am I doing wrong?







c# wpf asp.net-web-api






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 14:38









Rob

1,0091022




1,0091022










asked Nov 11 '18 at 21:08









Alex

14




14











  • Are you sure the route is ../api/account/Login and not ../api/Login?
    – Crowcoder
    Nov 11 '18 at 21:14










  • @Crowcoder Yes, I'm sure. I tried it with api/login, but I got "not found" error as the result. Again, api method was not called.
    – Alex
    Nov 11 '18 at 21:16










  • Turn on break on all CLR exceptions and debug to see where it is throwing an error.
    – Crowcoder
    Nov 11 '18 at 21:18










  • @Crowcoder The problem is the application does not break at all, I'm just not being able to login.
    – Alex
    Nov 11 '18 at 21:28










  • If you are getting a 500 error it will break if you turn it on in the exception settings. This is a way to see where an exception is thrown if it is otherwise swallowed. I'm betting it will break somewhere in the pipeline before it even gets to your controller.
    – Crowcoder
    Nov 11 '18 at 21:30
















  • Are you sure the route is ../api/account/Login and not ../api/Login?
    – Crowcoder
    Nov 11 '18 at 21:14










  • @Crowcoder Yes, I'm sure. I tried it with api/login, but I got "not found" error as the result. Again, api method was not called.
    – Alex
    Nov 11 '18 at 21:16










  • Turn on break on all CLR exceptions and debug to see where it is throwing an error.
    – Crowcoder
    Nov 11 '18 at 21:18










  • @Crowcoder The problem is the application does not break at all, I'm just not being able to login.
    – Alex
    Nov 11 '18 at 21:28










  • If you are getting a 500 error it will break if you turn it on in the exception settings. This is a way to see where an exception is thrown if it is otherwise swallowed. I'm betting it will break somewhere in the pipeline before it even gets to your controller.
    – Crowcoder
    Nov 11 '18 at 21:30















Are you sure the route is ../api/account/Login and not ../api/Login?
– Crowcoder
Nov 11 '18 at 21:14




Are you sure the route is ../api/account/Login and not ../api/Login?
– Crowcoder
Nov 11 '18 at 21:14












@Crowcoder Yes, I'm sure. I tried it with api/login, but I got "not found" error as the result. Again, api method was not called.
– Alex
Nov 11 '18 at 21:16




@Crowcoder Yes, I'm sure. I tried it with api/login, but I got "not found" error as the result. Again, api method was not called.
– Alex
Nov 11 '18 at 21:16












Turn on break on all CLR exceptions and debug to see where it is throwing an error.
– Crowcoder
Nov 11 '18 at 21:18




Turn on break on all CLR exceptions and debug to see where it is throwing an error.
– Crowcoder
Nov 11 '18 at 21:18












@Crowcoder The problem is the application does not break at all, I'm just not being able to login.
– Alex
Nov 11 '18 at 21:28




@Crowcoder The problem is the application does not break at all, I'm just not being able to login.
– Alex
Nov 11 '18 at 21:28












If you are getting a 500 error it will break if you turn it on in the exception settings. This is a way to see where an exception is thrown if it is otherwise swallowed. I'm betting it will break somewhere in the pipeline before it even gets to your controller.
– Crowcoder
Nov 11 '18 at 21:30




If you are getting a 500 error it will break if you turn it on in the exception settings. This is a way to see where an exception is thrown if it is otherwise swallowed. I'm betting it will break somewhere in the pipeline before it even gets to your controller.
– Crowcoder
Nov 11 '18 at 21:30












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253250%2fwebapi-500-internal-server-error-not-calling-api-method%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253250%2fwebapi-500-internal-server-error-not-calling-api-method%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20