找不到基本控制器动作,抛出404

时间:2014-03-07 21:48:39

标签: c# asp.net-mvc controller

我有一个基本控制器,每个页面都需要一个方法。它看起来像这样:

[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。

1 个答案:

答案 0 :(得分:0)

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

此外,您将post请求重定向到index action,如果示例显示整个类,则表示您没有任何索引操作。这就是你得到404的原因。

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

$ .post('/ Base / SetCurrent /'+ $(this).val());这是一个有效的电话。你应该在没有Controller的情况下编写类名。

相关问题