ASP.NET MVC控制器方法通过GetJson在服务器上不起作用

发布于 2025-02-09 19:07:52 字数 1603 浏览 2 评论 0原文

强制性“这在我的开发环境中有效,但在服务器上不起作用。”

我使用.NET Core有一个ASP.NET 5 MVC项目,其中在我的JavaScript代码中触发“ getjson”调用的操作又触发了我的JavaScript代码,该call又调用了控制器上的函数以从服务器中获取数据,为了更新页面而无需寄回。

当在文本框中进行有效的条目时,我的JavaScript中调用此函数:

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

...在我的家庭控制器中调用函数getBillingCustomerByCustnum:

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

    return Json(data);
}

在我的开发环境中,这很棒。但是,在Windows Server 2016计算机上将应用程序发布到IIS环境之后,这将失败。 II似乎正在尝试调用'/home/getBillingCustomerByCustnum',就像它是视图或物理对象一样,因此返回404错误。

我尝试通过添加来更改我的getjson控制器调用 - jQuery函数“ getCustomerOptions” - 以使

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

调用变为,

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

但仍然失败。根据调试器控制台的说法,上述URL被翻译为

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

我能找到的唯一另一个步骤,建议我用HTML辅助器准备URL,就像这样:

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

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

但是当然会失败,因为HTML帮助者无法在单独的JavaScript文件中工作。 var url从字面上写成。

最后,我知道这不是我的JavaScript文件未正确链接的情况,因为我可以在此失败的呼叫之前在调试器中断点我的JavaScript,并且在运行时击中断点。

我还能尝试解决什么?任何建议将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2025-02-16 19:07:52

您是否尝试过简单的主页/getBillingCustomerByCustnum,只是没有开始/?从您描述生产中的错误的方式,这基本上是服务器问题时的最终路由到控制器的问题。

删除home/零件有效,因为您正在从位于同一控制器的文件夹路径上的视图中调用该操作。由于您使用的是.NET Core,因此我建议使用asp-actionasp-controller标记助手,因为他们让服务器决定到所需方法的实际路线是什么,即使您发布获取,而无需实际寄回。例如,这是我使用JavaScript在表单上调用我的方法的操作:

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

这就是我获取JS代码来检索相应URL的方式

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

。表单没有实际提交按钮,使呼叫都是由JavaScript进行的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文