Home > Software design >  ASP.NET MVC controller method called via getJson not working on server
ASP.NET MVC controller method called via getJson not working on server

Time:06-24

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/<%= Url.Content("~/") %>/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.

CodePudding user response:

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.

  • Related