【问题标题】:RedirectToAction and "Object moved to here" errorRedirectToAction 和“对象移至此处”错误
【发布时间】:2012-04-05 15:53:36
【问题描述】:

我在 MVC 3.0 中遇到了一个奇怪的 RedirectToAction 问题。

这是我的示例 ViewModel 的代码

public class EventViewModel
{
  [Required(ErrorMessageResourceType = typeof(Resources.Validations), ErrorMessageResourceName = "Required")]
  public DateTime CreationDate { get; set; }

  [Required(ErrorMessageResourceType = typeof(Resources.Validations), ErrorMessageResourceName = "Required")]
  [AllowHtml] //here is my apparent problem
  public string Description { get; set; }

  [Required(ErrorMessageResourceType = typeof(Resources.Validations), ErrorMessageResourceName = "Required")]
  [Range(0, 5, ErrorMessageResourceType = typeof(Resources.Validations), ErrorMessageResourceName = "RangeValue")]
  public int Rating { get; set; }
  [Required(ErrorMessageResourceType = typeof(Resources.Validations), ErrorMessageResourceName = "Required")]
  public string Title{ get; set; }

  ...other properties...

}

这是我的控制器的两个方法

public ActionResult Edit(int id)
{
  var entity = eventsRepository.Get(id);
  if (entity == null)
    return RedirectToAction("Index");
  var eventVM = new EventViewModel();
  eventVM.Description = entity.Description;

  ... set the other properties ...

  return View(eventVM);
}

[HttpPost]
public ActionResult Edit(int id, EventViewModel model)
{
  if (ModelState.IsValid)
  {
    try
    {
      var entity = eventsRepository.Get(id);
      if (entity == null)
        return RedirectToAction("Index");
      entity.CreationDate = model.CreationDate;
      entity.Description = model.Description;

      ... set the other properties ...

      eventsRepository.Save(entity);
      return RedirectToAction("Index");
    }
    catch (Exception e)
    {
      ModelState.AddModelError("", "An error occured bla bla bla");
    }
  }

  return View(model);
}

我的问题是,如果我删除 AllowHtmlAttribute 并在描述字段中插入纯文本,一切正常,保存后我会获得重定向,但如果我将 AllowHtmlAttribute 放在字段描述中并插入一些 Html文本,保存而不是重定向后,我得到一个只有以下文本的空白页:

Object moved to here. 

如果我点击“这里”,我会被重定向到正确的网址。

我是否遗漏了一些明显的东西?

【问题讨论】:

    标签: c# asp.net-mvc-3


    【解决方案1】:

    阅读此forum post。简而言之,如果在处理重定向之前修改了 http 标头,您将收到该错误消息。我不完全确定 [allowhtml] 属性是如何导致这种情况的,但我想至少它是一个跳跃点。

    【讨论】:

    • 我知道标头问题,并且使用 Fiddler 看我没有发现任何差异。猜猜我必须创建一个简单的项目并从头开始尝试找到真正的问题。我也是,我对allowhtml持怀疑态度,但如果我删除它一切正常,所以问题应该出在处理html值的方式上。
    • @Iridio,那也是我要走的路线。祝你好运解决它,当你弄明白时,一定要把答案发回这里。
    • 我从头开始使用假存储库创建了一个项目,并且所有工作都按预期进行。所以我开始假设 [AllowHtml] 属性还有一些其他的东西会发疯。也许是我的存储库的保存。 (不太可能,但这是我唯一的区别)当我没有更多时间时,我将再次重写我的代码并尝试找出真正的原因并回到这里。
    【解决方案2】:

    它实际上是由一个 httpModule 干扰 mvc 引起的。我有同样的问题,并通过从主 web.config 中删除以下内容来修复它。

    <add type="DevExpress.Web.ASPxClasses.ASPxHttpHandlerModule, DevExpress.Web.v11.1, Version=11.1.10.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" name="ASPxHttpHandlerModule" />
    

    【讨论】:

    • 感谢您解决了问题。我没有注意到在新的示例项目中我没有在 httpmodule 中包含 devexpress 的调用。
    【解决方案3】:

    RedirectToAction 将向浏览器发送一个新的 HTTP 响应。我不明白 ASP.Net 在幕后做什么,但显然它正在创建一个页面,其中包含指向您在 RedirectToAction 调用中指定的操作的链接。

    我并不完全清楚 Edit(int id, EventViewModel model) 中的实体是什么,但您似乎可以使用它来获取编辑视图所需的 EventViewModel

    Edit(int id, EventViewModel model)
    

    之后

    ... set the other properties ...
    
    eventsRepository.Save(entity);
    

    添加

    var eventVM = new EventViewModel();
    eventVM.Description = entity.Description;
    
    ... set the other properties ...
    
    return View(eventVM);
    

    【讨论】:

    • 我在示例代码中犯了一个错误。保存后我想重定向到索引而不是再次编辑。无论如何,在这两种情况下,我都会遇到相同的错误。相反,如果我按照您的建议返回 View(eventVM) 一切正常,但不是我需要的:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    相关资源
    最近更新 更多