在服务器 ajax 上部署 MVC 应用程序时无法找到操作,但 Localhost 可以
When deploying MVC app on server ajax cant find action but Localhost it does
我有一个 MVC5 应用程序。当我 运行 它在我的本地主机上时,一切正常,没有任何错误。
当我发布我的应用程序然后将其传输到 Windows Server 2016 时,我将文件放在 IIS 文件夹的 wwwroot 中,然后我 link 一切以在 IIS 中创建一个新网站。然后我 运行 该网站并且它有效。我让我的 javascript 代码可以工作,但是当我去 运行 我的 'ajax' 方法时,我得到一个 404 错误并且在函数中我找不到我的控制器操作所以该方法将工作。
这是我的实际错误:
xxx.xxx.xx x.219/Parts/DoPartBookFunc?bookval=8 404 (Not Found),
Failed to load resource: the server responded with a status of 404
(Not Found)
我一直在研究和尝试很多不同的东西,但到目前为止还没有成功。我试过的一些东西是
@Url.action(“”,””)
- 前面加一个~
- 在前面加上../
- 制作全局文件
还有很多其他的东西。如果有人知道如何解决这个问题,我们将不胜感激。
$("#PartBook").on("change", function () {
var selectV = $(this).val();
var selectT = $(this).text();
$.ajax({
url: '/Parts/DoPartBookFunc',
type: 'GET',
dataType: 'json',
data: { bookval: selectV },
//contentType: 'application/json; charset=utf-8',
success: function (data) {
//alert("s" + data.PartNextNumber);
这是我的解决方案大家!
将我的 ajax() url 更改为:
url: "@Url.Action("DoPartBookFunc", "Parts")",
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
/* Newly Added */
routes.MapRoute(
name: "AddNewControllerName",
url: "AddNewControllerName/{action}/{id}",
defaults: new { controller = "AddNewControllerName", action = "Index", id = UrlParameter.Optional }
);
/* Old Route */
routes.MapRoute(
name: "mass",
url: "{action}/{id}",
defaults: new { controller = "Parts", action = "Index", id = UrlParameter.Optional }
);
尝试从 url 中删除 /:
$("#PartBook").on("change", function () {
var selectV = $(this).val();
var selectT = $(this).text();
$.ajax({
url: 'Parts/DoPartBookFunc',
type: 'GET',
dataType: 'json',
data: { bookval: selectV },
//contentType: 'application/json; charset=utf-8',
success: function (data) {
//alert("s" + data.PartNextNumber);
...
})
或尝试添加:
var RootUrl = '@Url.Content("~/")';
$.ajax({
type: "POST",
url: RootUrl + "Parts/DoPartBookFunc",
我有一个 MVC5 应用程序。当我 运行 它在我的本地主机上时,一切正常,没有任何错误。
当我发布我的应用程序然后将其传输到 Windows Server 2016 时,我将文件放在 IIS 文件夹的 wwwroot 中,然后我 link 一切以在 IIS 中创建一个新网站。然后我 运行 该网站并且它有效。我让我的 javascript 代码可以工作,但是当我去 运行 我的 'ajax' 方法时,我得到一个 404 错误并且在函数中我找不到我的控制器操作所以该方法将工作。
这是我的实际错误:
xxx.xxx.xx x.219/Parts/DoPartBookFunc?bookval=8 404 (Not Found), Failed to load resource: the server responded with a status of 404 (Not Found)
我一直在研究和尝试很多不同的东西,但到目前为止还没有成功。我试过的一些东西是
@Url.action(“”,””)
- 前面加一个~
- 在前面加上../
- 制作全局文件
还有很多其他的东西。如果有人知道如何解决这个问题,我们将不胜感激。
$("#PartBook").on("change", function () {
var selectV = $(this).val();
var selectT = $(this).text();
$.ajax({
url: '/Parts/DoPartBookFunc',
type: 'GET',
dataType: 'json',
data: { bookval: selectV },
//contentType: 'application/json; charset=utf-8',
success: function (data) {
//alert("s" + data.PartNextNumber);
这是我的解决方案大家!
将我的 ajax() url 更改为:
url: "@Url.Action("DoPartBookFunc", "Parts")",
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
/* Newly Added */
routes.MapRoute(
name: "AddNewControllerName",
url: "AddNewControllerName/{action}/{id}",
defaults: new { controller = "AddNewControllerName", action = "Index", id = UrlParameter.Optional }
);
/* Old Route */
routes.MapRoute(
name: "mass",
url: "{action}/{id}",
defaults: new { controller = "Parts", action = "Index", id = UrlParameter.Optional }
);
尝试从 url 中删除 /:
$("#PartBook").on("change", function () {
var selectV = $(this).val();
var selectT = $(this).text();
$.ajax({
url: 'Parts/DoPartBookFunc',
type: 'GET',
dataType: 'json',
data: { bookval: selectV },
//contentType: 'application/json; charset=utf-8',
success: function (data) {
//alert("s" + data.PartNextNumber);
...
})
或尝试添加:
var RootUrl = '@Url.Content("~/")';
$.ajax({
type: "POST",
url: RootUrl + "Parts/DoPartBookFunc",