0

I'm building an app ("Airport Finder" or "af" for the URL) on my personal server at home. When I run the app from VS, it works perfectly. However, when I run it through the browser, it doesn't.

The controller has two actions. The first one provides data for building the home page when it's initially displayed. That action works great. It's called as part of the razor view that returns the home page when you first hit the site.

However, the second action is returning a 404 (Not Found). It's called via AJAX after the user selects various criteria on the website. Again, this one works fine, too, if you do the search after running the website from within Visual Studio (2017). It returns the 404 if I run the site straight from the browser (although the first action returns all the initial data just fine).

I've installed the app in IIS (deployed to a folder and created an app under the default website which points at that folder).

The URL looks like this:

http://localhost/af

but the Search URL looks like this when I get the error:

http://localhost/Home/Search

I would've thought it should look like:

http://localhost/af/Home/Search

but, entering that into the browser manually just returns another 404 (Not Found).

Here's the Controller code:

    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View(Logic.GetGeneralAirportData());
        }

        [HttpPost]
        public JsonResult Search(SearchCriteria sc)
        {
            SearchResults results = Logic.Search(sc);
            return Json(results);
        }
    }

Here's the AJAX call:

        $.ajax({
            method: "post",
            url: "/Home/Search",
            dataType: "json",
            data: {
...

Here's the route:

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
birdus
  • 7,062
  • 17
  • 59
  • 89
  • What happens if you call : http://localhost/af/Home/Search? – Robert Jan 02 '19 at 19:30
  • @Robert 404. I'll add that info above. Thanks. – birdus Jan 02 '19 at 19:40
  • 1
    Does the data your passing match the object coming in? – Robert Jan 02 '19 at 19:42
  • What do your MVC Routes look like? – Brandon Turpy Jan 02 '19 at 19:45
  • @Robert If you're asking if the data that I'm building up in the AJAX call match up with the C# class parameter in the action, then yes. Remember, this works perfectly when run from within VS. If that's not what you mean, then please clarify. – birdus Jan 02 '19 at 19:46
  • @BrandonTurpy Not a strong suit of mine. Added at the bottom of the question. – birdus Jan 02 '19 at 19:47
  • 1
    When you say you run it through the browser, how are you running it? Do you start it up in command prompt? IIS? – Brandon Turpy Jan 02 '19 at 19:58
  • I've installed the app in IIS (deployed to a folder and created an app under the default website which points at that folder). – birdus Jan 02 '19 at 20:02
  • Have you inspected the [IIS log files](https://stackoverflow.com/questions/6426375/where-can-i-find-the-iis-logs) to see what the error is? – Ryan Jan 03 '19 at 03:28
  • What is your application site name when deploying in the IIS? After deploying, can you access `Home/Index`? Try to access `http:localhost/yoursitename/Home/Index`. – Edward Jan 03 '19 at 05:44
  • @TaoZhou I'm not sure what you mean by "application site name." http://localhost/af/Home/Index runs fine. – birdus Jan 03 '19 at 15:30
  • @XingZou The logs don't show any errors. – birdus Jan 03 '19 at 15:56

1 Answers1

0

Not sure if this will solve the problem. But personally I like to have different controllers for AJAX calls. If it were me I would have a separate folder for api controllers so you have

ApiControllers
   - HomeApiController

Controllers
   - HomeController

And any ajax calls I make in the home controller will then call the home api controller which would look something like this.

[Route("api/Home")]
[ApiController]
public class HomeApiController : ControllerBase
{
  // PRIVATE VARIABLES

    public HomeApiController(// DEPENDENCIES)
    {
        // ASSIGN DEPENDENCIES TO PRIVATE VARIABLES
    }

    [HttpPost("Search")]
    public string Search(SearchCriteria sc)
    {
        SearchResults results = Logic.Search(sc);
        return Json(results);
    }  
  }

Then the path would be the base url + /api/home/search

Brandon Turpy
  • 883
  • 1
  • 10
  • 32
  • If I do this (which I think is a good idea), I get a 403 (Forbidden) when run through the browser. However, I put the route at the action level rather than class level. The error is definitely on the new route: http://localhost/api/home/search. Maybe that's a step in the right direction. The code DOES work when run via VS. – birdus Jan 02 '19 at 20:48