2

我正在使用 Ajax.BeginForm 创建一个表单,该表单将对某个控制器操作执行 ajax 回发,然后将响应视图插入到 UpdateTargetId 中。

using (Ajax.BeginForm("Save", null,
        new { userId = Model.UserId },
        new AjaxOptions { UpdateTargetId = "UserForm" },
        new { name = "SaveForm", id = "SaveForm" }))
   {
    [HTML SAVE BUTTON]
   }

一切都很好,除非用户会话超时,然后他们被重定向回登录页面。然后,由于 Authorize 属性,登录页面从 Ajax 调用返回,并且 html 被加载到 UpdateTargetId 中,因此我最终在用户配置文件页面中(在目标 Id 处)得到登录页面 html。我的控制器操作如下所示:

[Authorize]
public ActionResult Save(Int32 UserId)
{
    //code to save user
    return View("UserInfoControl", m);

}

我怎么解决这个问题?

更新(2011-10-20):从 Phil Haack找到这篇关于这个确切问题的帖子 - http://haacked.com/archive/2011/10/04/prevent-forms-authentication-login-page-redirect-when-你-donrsquot-want.aspx。我还没有机会消化或实施他的解决方案。

4

1 回答 1

0

我认为您可以从操作内部处理授权问题。首先删除[Authorize],使用以下代码:

public ActionResult Save(Int32 UserId)
{
    if (!User.Identity.IsAuthenticated) 
    { 
        throw new Exception();
    }
    //code to save user
    return View("UserInfoControl", m);

}

然后你可以在你的 AjaxOptions 中放置一个 OnFailure 条件并重定向你的页面或其他东西。

于 2009-10-08T20:53:41.503 回答