【问题标题】:Base Controller Action not being found, throwing 404找不到基本控制器操作,抛出 404
【发布时间】:2014-03-07 21:48:39
【问题描述】:

我有一个基本控制器,其中包含我在每个页面上都需要的方法。它看起来像这样:

[Authorize]
public class BaseController : Controller
{
    [HttpPost]
    public ActionResult SetCurrent(String value)
    {
        Session["current-id"] = value;

        return RedirectToAction("Index");
    }
}

我从这个类派生了我的其他控制器。

我需要能够随时从任何页面调用 SetCurrent 并刷新当前页面。

但是现在,任何试图使用它的控制器都无法识别 SetCurrent。

它会抛出 404。

有没有更好的方法来做到这一点?

编辑:添加我用来调用 URL 的代码

$.post('SetCurrent/' + $(this).val());

这是在 jquery 中的一个选择输入。这部分工作正常,带我去:

http://localhost:49191/SetCurrent/eaa2ba63-cfc6-4572-8b28-4b95df20e7e4

给我:

Failed to load resource: the server responded with a status of 404 (Not Found) 

编辑 2:这些是我的地图路线:

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{value}",
            defaults: new { controller = "Dashboard", action = "Index", value = UrlParameter.Optional }
        );

另外,如果我将调用更改为:

$.post('/Base/SetCurrent/' + $(this).val()); 

它仍然会抛出 404。

【问题讨论】:

  • 既然有 [HttpPost] - 你确定在调用方实际使用 POST 而不是 GET 吗?
  • 你能显示你调用它的代码/表单吗?如果没有,那么如果它只是一个 url 导航,你需要做GET
  • 最后加上调用代码
  • 要么您没有显示任何 MapRoutes,要么您的 URL 缺少控制器。
  • 我刚刚添加了我的 MapRoute。它通常工作得很好。只是无法识别 BaseController。

标签: c# asp.net-mvc controller


【解决方案1】:

尝试使用 Firebug 检查请求和等效响应,但这看起来像根路径问题。

您还将发布请求重定向到索引操作,如果示例显示整个班级您没有任何索引操作。这就是你得到 404 的原因。

而且我认为 ajax 调用的重定向是一种不好的做法。

$.post('/Base/SetCurrent/' + $(this).val());这是一个有效的电话。你应该写没有控制器的类名。

【讨论】:

    猜你喜欢
    • 2017-11-01
    • 1970-01-01
    • 2013-04-07
    • 2017-12-21
    • 2011-10-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多