Datatables Working on Local Host but when published to Web app get error 404
Datatables Working on Local Host but when published to Web app get error 404
Hi,
I have an issue when using aspnet.core 2.1, I have datatables controller for my database which connects to the azure hosted sql server database.
This works fine when using IIS Express Localhost however when I publish the project to a web application I get an the following error,
DataTables warning: table id=example - Ajax error. For more information about this error, please see http://datatables.net/tn/7.
The connection string I am trying to use has windows login and I believe this error may be due to the api request (http post and get) as it says the api url cannot be found error 404 or possibly due to the antiforgery token in the ajax request?
Answers
You will need to look at your server logs and configuration to determine the exact cause of the 404. The Cross-Site Request Forgery docs may be helpful.
Kevin
It is still not finding the api Url from the controller?
The 404 error is returned from your server. That is the place to look to find out why the server is responding with the 404. The server logs should provide details of why the error is occurring.
Kevin
Yes the server logs show the error is in the ajax request and is now giving a status code 400 Bad Request
Do the server logs indicate why its a bad request?
Maybe this troubleshooting link will help:
https://airbrake.io/blog/http-errors/400-bad-request
Kevin
No it does not appear to indicate why
Could you show me your controller code please? Most likely its something to do with the routing, but I'm not sure what. The controller will be where we can start though.
Allan
[Route("/Issues/Data")]
[HttpGet]
[HttpPost]
Thank - so if you load:
in your browser, what do you get? Is it still a 404 or 400 error?
Allan
404 error still
Okay good - so its a routing issue.
I guess the key question now is what is the difference between your local ISS Express server and the remote server? What versions of ISS are they running? What versions of .NET Framework, etc?
Allan
The 400 Bad Request error is an HTTP status code indicates that the request you sent to the webserver was malformed , in other words, the data stream sent by the client to the server didn't follow the rules. It means that the request itself has somehow incorrect or corrupted and the server couldn't understand it. There are a number of different causes for a 400 Bad Request Error . It might be a malformed request syntax, invalid request message framing, or deceptive request routing . In most cases, the problem is on the website itself, and there's not much you can do about that.
In my case that was an error linked to the IIS configuration. The app was ASP.NET Core and deployed in virtual directory under the default web app(Instead of localhost:5000 it was deployed in localhost:5000/application). It means that ASP.NET Core links works fine but datatables links were broken(You could try to check this fact using Fiddler). This issue was solved with adding app path to the links in views.
@model HomeViewModel
@{string appPath= $"{Context.Request.Scheme}://{Context.Request.Host.Value}{Url.Content("~/")}";}
and using them in url for datatables
"ajax": {
"url": "@(appPath)" + "Uploads/LoadData",
"type": "POST",
"datatype": "json"
}