ASP.NET Core Action works from VS, but not from browser












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?}");
});









share|improve this question

























  • What happens if you call : localhost/af/Home/Search?

    – Robert
    Jan 2 at 19:30











  • @Robert 404. I'll add that info above. Thanks.

    – birdus
    Jan 2 at 19:40






  • 1





    Does the data your passing match the object coming in?

    – Robert
    Jan 2 at 19:42











  • What do your MVC Routes look like?

    – Brandon Turpy
    Jan 2 at 19:45






  • 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 2 at 19:58


















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?}");
});









share|improve this question

























  • What happens if you call : localhost/af/Home/Search?

    – Robert
    Jan 2 at 19:30











  • @Robert 404. I'll add that info above. Thanks.

    – birdus
    Jan 2 at 19:40






  • 1





    Does the data your passing match the object coming in?

    – Robert
    Jan 2 at 19:42











  • What do your MVC Routes look like?

    – Brandon Turpy
    Jan 2 at 19:45






  • 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 2 at 19:58
















0












0








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?}");
});









share|improve this question
















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?}");
});






ajax asp.net-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 20:02







birdus

















asked Jan 2 at 19:25









birdusbirdus

2,525124980




2,525124980













  • What happens if you call : localhost/af/Home/Search?

    – Robert
    Jan 2 at 19:30











  • @Robert 404. I'll add that info above. Thanks.

    – birdus
    Jan 2 at 19:40






  • 1





    Does the data your passing match the object coming in?

    – Robert
    Jan 2 at 19:42











  • What do your MVC Routes look like?

    – Brandon Turpy
    Jan 2 at 19:45






  • 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 2 at 19:58





















  • What happens if you call : localhost/af/Home/Search?

    – Robert
    Jan 2 at 19:30











  • @Robert 404. I'll add that info above. Thanks.

    – birdus
    Jan 2 at 19:40






  • 1





    Does the data your passing match the object coming in?

    – Robert
    Jan 2 at 19:42











  • What do your MVC Routes look like?

    – Brandon Turpy
    Jan 2 at 19:45






  • 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 2 at 19:58



















What happens if you call : localhost/af/Home/Search?

– Robert
Jan 2 at 19:30





What happens if you call : localhost/af/Home/Search?

– Robert
Jan 2 at 19:30













@Robert 404. I'll add that info above. Thanks.

– birdus
Jan 2 at 19:40





@Robert 404. I'll add that info above. Thanks.

– birdus
Jan 2 at 19:40




1




1





Does the data your passing match the object coming in?

– Robert
Jan 2 at 19:42





Does the data your passing match the object coming in?

– Robert
Jan 2 at 19:42













What do your MVC Routes look like?

– Brandon Turpy
Jan 2 at 19:45





What do your MVC Routes look like?

– Brandon Turpy
Jan 2 at 19:45




1




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 2 at 19:58







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 2 at 19:58














1 Answer
1






active

oldest

votes


















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






share|improve this answer
























  • 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: localhost/api/home/search. Maybe that's a step in the right direction. The code DOES work when run via VS.

    – birdus
    Jan 2 at 20:48














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%2f54012074%2fasp-net-core-action-works-from-vs-but-not-from-browser%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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






share|improve this answer
























  • 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: localhost/api/home/search. Maybe that's a step in the right direction. The code DOES work when run via VS.

    – birdus
    Jan 2 at 20:48


















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






share|improve this answer
























  • 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: localhost/api/home/search. Maybe that's a step in the right direction. The code DOES work when run via VS.

    – birdus
    Jan 2 at 20:48
















0












0








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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 20:20









Brandon TurpyBrandon Turpy

398120




398120













  • 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: localhost/api/home/search. Maybe that's a step in the right direction. The code DOES work when run via VS.

    – birdus
    Jan 2 at 20:48





















  • 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: localhost/api/home/search. Maybe that's a step in the right direction. The code DOES work when run via VS.

    – birdus
    Jan 2 at 20:48



















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: localhost/api/home/search. Maybe that's a step in the right direction. The code DOES work when run via VS.

– birdus
Jan 2 at 20:48







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: localhost/api/home/search. Maybe that's a step in the right direction. The code DOES work when run via VS.

– birdus
Jan 2 at 20:48






















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012074%2fasp-net-core-action-works-from-vs-but-not-from-browser%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

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$