【发布时间】: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