【问题标题】:When deploying MVC app on server ajax cant find action but Localhost it does在服务器 ajax 上部署 MVC 应用程序时找不到操作,但 Localhost 可以
【发布时间】:2019-11-21 06:04:14
【问题描述】:

我有一个 MVC5 应用程序。当我在本地主机上运行它时,一切正常,没有任何错误。

当我发布我的应用程序然后将其传输到 Windows Server 2016 时,我将文件放在 IIS 文件夹中的 wwwroot 中,并链接所有内容以在 IIS 中创建一个新网站。然后我运行该网站并且它可以工作。我让我的 javascript 代码工作,但是当我去运行我的“ajax”方法时,我得到一个 404 错误,并且在函数中我找不到我的控制器操作,所以该方法可以工作。

这是我的实际错误:

xxx.xxx.xx x.219/Parts/DoPartBookFunc?bookval=8 404(未找到), 加载资源失败:服务器响应状态为 404 (未找到)

我一直在研究和尝试很多不同的东西,但到目前为止还没有运气。我尝试过的一些事情是

  • @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.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 }
            ); 

【问题讨论】:

标签: c# asp.net-mvc-5 http-status-code-404 web-deployment


【解决方案1】:

尝试从 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",

【讨论】:

  • 这就是解决它的方法。我把它混在 url: "@Url.Action("AjaxDescriptionSearch", "Parts")",
  • 哦,你还必须在我的 RouteConfig 中添加一个路由 /* 新添加的 / routes.MapRoute( name: "AddNewControllerName", url: "AddNewControllerName/{action}/{id}" , 默认值:new { controller = "AddNewControllerName", action = "Index", id = UrlParameter.Optional } ); / 旧路线 */ routes.MapRoute( name: "mass", url: "{action}/{id}", defaults: new { controller = "Parts", action = "Index", id = UrlParameter .可选的});
  • 我建议您将最终解决方案放入您的问题的编辑中:)
猜你喜欢
  • 1970-01-01
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 2012-07-04
  • 2020-10-07
  • 2018-02-01
  • 1970-01-01
相关资源
最近更新 更多