【问题标题】:404 Error in controller action methods after deploying to remote server部署到远程服务器后,控制器操作方法中出现 404 错误
【发布时间】:2020-05-04 21:39:08
【问题描述】:

.JS 文件

$("#drpdwn").change(function ()
{
    var filter1 = document.getElementById("drpdwn").value;
    window.location = '/Controller/GetByFilter?filter=' + filter1;
});

控制器中的操作方法:

public ActionResult GetByFilter(string filter)
{
    var model = obj.GetByFilter(filter);
    return View(model);
}

这会在服务器上引发 404,但在 localhost 上可以正常工作。问题出在 URL 部分,我不知道如何解决它。

【问题讨论】:

    标签: javascript c# asp.net model-view-controller


    【解决方案1】:

    当您在远程服务器上进行分解时,很少有事情发生,如果您在根目录下部署,那么它将起作用,即http://www.example.com

    但是如果你添加了应用程序或虚拟目录,那么你也需要使用虚拟目录。

    i.e. window.location = '/[virdir]/Controller/GetByFilter?filter=' + filter1;
    
    So best option will be use absolute url instead of relative to the application.
    
    i.e. window.location = 'http://www.example.com/Controller/GetByFilter?filter=' +filter1;
    
    or window.location = 'http://www.example.com/myapp/Controller/GetByFilter?filter=' +filter1;
    

    【讨论】:

    • 我不能像这样对值进行硬编码,因为这将部署在不同的环境中......有没有更好的方法来做到这一点?
    • 绝对url可以使用web.config,每个站点都可以更改,无需重新发布。
    【解决方案2】:

    你可以使用这样的东西

    window.location.href = "@Url.Action("ActionName","ControllerName")";

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-28
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 2010-12-31
      • 2015-03-31
      相关资源
      最近更新 更多