0

Obligatory "This works in my dev environment, but doesn't work on the server."

I have an ASP.NET 5 MVC project, using .NET Core, in which actions taken on a certain view trigger a "getJson" call in my javascript code, which in turn calls a function on the controller to obtain data from the server, in order to update the page without a postback.

When a valid entry is made in a textbox, this function is called in my javascript:

function getCustomerOptions(cn) {
    $.getJSON('/Home/GetBillingCustomerByCustNum', { cust: cn }, function (data) {
        // handle returned json data
    }
}

... which calls function GetBillingCustomerByCustNum in my Home controller:

public async Task<JsonResult> GetBillingCustomerByCustNum(string cust)
{
    var data = //[retrieve data from server thru repository function]

    return Json(data);
}

In my dev environment, this works great. But after I publish the application to an IIS environment on a Windows Server 2016 machine, this fails. It seems that IIS is trying to call '/Home/GetBillingCustomerByCustNum' as though it were a view or a physical object, and so returns a 404 error.

I have tried altering my getJson controller call -- jquery function "getCustomerOptions" -- by adding

<%= Url.Content("~/") %>

so that the call becomes

$.getJSON('<%= Url.Content("~/") %>/Home/GetBillingCustomerByCustNum', { cust: cn }, function (data) { ...

but that still fails. According to the debugger console, the above url is being translated as

http://localhost/HC_RFQ/Home/%3C%=%20Url.Content(%22~/%22)%20%%3E/Home/GetBillingCustomerByCustNum?cust=[given value]

The only other step I could find suggested I prepare my url with an HTML helper, like so:

var url = '@Url.Content("~/Home/GetBillingCustomerByCustNum/")'

$.getJSON(url, { cust: cn }, function (data) {...

but of course that fails because HTML helpers don't work in separate javascript files. Var url is passed in as literally written.

Finally, I know this is not a case of my javascript files not being linked properly, because I can breakpoint my javascript in the debugger ahead of this failing call, and the breakpoints are hit at runtime.

What else can I try to fix this? Any advice is appreciated.

4
  • 1
    Are you using ASP.NET MVC 5 on the full/classic .NET framework, or is it ASP.NET Core 5 MVC on the newer, cross-platform .NET Core framework? Not quite clear from the way you asked the question (and the missing tags also don't really help.....) Commented Jun 22, 2022 at 18:53
  • @marc_s Fair point. I am using .net core, and I've edited the first paragraph to make that clear. Commented Jun 23, 2022 at 12:07
  • Have you tried a simple Home/GetBillingCustomerByCustNum, just without the starting /? From how you describe the error in production, that's basically a server issue when composing the final route to the controller. Commented Jun 23, 2022 at 13:26
  • 1
    @Davide Vitali That did it! Actually, I had to drop the "/Home/" part of the url altogether, but the call is working now. Post this as an answer if you want, and I'll give you the credit. Otherwise I'll post the answer myself. Commented Jun 23, 2022 at 13:53

1 Answer 1

1

Have you tried a simple Home/GetBillingCustomerByCustNum, just without the starting /? From how you describe the error in production, that's basically a server issue when composing the final route to the controller.

Dropping the Home/ part works because you're calling that action from a view that resides on the same controller's folder path. Since you're using .NET Core, I suggest using the asp-action and asp-controller tag helpers as they let the server decide what's the actual route to the desired methods, even if you're POSTing or GETing without an actual postbacks. For example, this is what I do using javascript to call my methods on a form:

<form asp-controller="myController" asp-action="myAction">

and this is how I get my js code to retrive the corresponding url

let form = $(this).parents('form')[0];
let url = form.getAttribute('action');

the form doesn't have an actual submit button, so that the calls are all made from javascript.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.