Im creating a ASP.NET Core 3.1 site and have now published a test version to a azure app service.
When I run my web app on localhost it works just fine, but when I run the published app it seems like some of the actions does not exist anymore (I know they exist, otherwise it would be compilation errors) and all I get back is a 404 not found.
An example of a action that goes rouge when I publish is "LogIn" that takes a email and a code and is located in HomeController:
public IActionResult LogIn(string email, string code)
{
//Code
return Json(jObject);
}
This action is called via this Ajax code:
$.ajax({
type: "GET",
url: "/Home/LogIn",
data: { email: email, code: code },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
},
error: function (response) {
console.log(response);
}
});
The ajax call returns 404 not found on the published app. But I have also tried to do the same thing by creating a "a" tag:
<a asp-action="LogIn" asp-controller="Home" asp-route-email="test@test.com" asp-route-code="123">Login</a>
But that returns 404 not found as well...
Added Route value to the action (and the controller):
[Route("Home/LogIn")]
public IActionResult LogIn(string email, string code)
{
//Code
return Json(jObject);
}
Moved the ajax code to the .cshtml file and changed url to:
url: "@Url.Action("LogIn", "Home")"
But none of these solutions solve my problem...
Is there anyone that recognize this problem? Does anyone have a solution for it? I would really appreciate some help.
Thanks in advance!
EDIT:
I have tested to remove all content of the "LogIn" action and replaced it with "return View()" and created a view for it, and it works fine. The "LogIn" action can be found via the ajax call as well, but when I add the code I want there again and publishing, the action gets a 404 not found again...
I then tried to add [HttpGet] to the action and it resulted in a 405 method not allowed. So I think it sees the action but ignore it or something similar.
I solved the problem.
I was looking at the azure app service log and found this when i did the call:
2020-12-04 19:14:57.864 +00:00 [Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Route matched with {action = "LogIn", controller = "Home"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult LogIn(System.String, System.String) on controller Project.Controllers.HomeController.
And further down in the log stream this showed up:
2020-12-04 19:14:57.929 +00:00 [Error] Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware: An unhandled exception has occurred while executing the request.System.Data.SqlClient.SqlException (0x80131904): Cannot open server 'mysrvname' requested by the login. Client with IP address 'appserviceIP' is not allowed to access the server.
So the problem was all the actions that made database calls "was not found" but they actually got sqlexecptions beacuse i forgot to update my server firewall rules to accept my app services IP adress.
Really wierd that I got a 404 not found when I should get some server error...