【问题标题】:Asp.net MVC AcceptVerbsAsp.net MVC AcceptVerbs
【发布时间】:2009-07-08 13:02:36
【问题描述】:

这里RedirectToAction() 不调用ActionResult 索引。我需要为此注册路线吗?

[ActionName("onchange")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult OnChange(int i)
{
    m_NumberOfVisibleItems = i;           
    return RedirectToAction("Index");
}

【问题讨论】:

  • 你能显示索引操作方法代码吗?
  • public ActionResult Index() {..... return View(s_Model); }

标签: asp.net-mvc routing acceptverbs


【解决方案1】:

根据您使用的 MVC 版本,您不再需要 [ActionName]。另外,您确定您在该控制器中有一个“索引”操作,并且您没有如下所示的 [ActionName]:

[ActionName("index")]

MVC 中很多东西的工作方式是通过反射——我相信大写可能是一个问题。

【讨论】:

  • 嗨,我也试过了,如下所示,但还是不行...... [ActionName("index")] [AcceptVerbs(HttpVerbs.Get)] public ActionResult Index() {.. ..} [ActionName("onchange")] [AcceptVerbs(HttpVerbs.Post)] public ActionResult OnChange(int i) { m_NumberOfVisibleItems = i; return RedirectToAction("index"); }
  • 如果您正在运行 MVC 的 RC,则不再需要 [ActionName] 装饰器。尝试摆脱它们。也尝试只返回视图: return View("Index", );
【解决方案2】:

我不是 100% 确定,但 RedirectToAction 不会向客户端发送 302 重定向吗?所以它不会直接调用Index(),只会在来自浏览器的下一个请求中。

【讨论】:

  • 你是对的 - 这是一个实际的重定向,因此,应该从头开始调用 Index 动作......所以无论你如何到达那里,都应该调用 Index 动作。如果没有更多信息,它甚至可能是带有某种重定向设置问题的 IIS
猜你喜欢
  • 2010-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多