开发者

Custom ASP.NET MVC ActionResult is not executed

I've used t开发者_开发技巧he PermanentRedirectResult from here to perform 301 redirects in ASP.NET MVC 1. Since upgrading to 2.0, the 301 response is no longer sent. Instead, a 200 response containing just the class name is sent.

Setting a breakpoint indicates that the ExecuteResult method on PermanentRedirectResult is never called. Apparently the framework is just calling ToString() on the result instead of executing it.

Has anyone else seen this? Is this a deliberate change in 2.0, and if so what do I now need to do to use a custom ActionResult?


Reworked a bit from Steve Sanderson's Pro ASP.NET MVC 2 Framework which I highly recommend you buy. There's no better resource.

public class HomeController : Controller
{
  public ActionResult Index()
  {
    return RedirectToAction("Other").MakePermanent();
  }

  public ActionResult Other()
  {
    return View();
  }
}

public static class RedirectExtensions
{
  public static PermRedirectToRouteResult MakePermanent(this RedirectToRouteResult redirect)
  {
    return new PermRedirectToRouteResult(redirect);
  }

  public class PermRedirectToRouteResult : ActionResult
  {
    public RedirectToRouteResult Redirect { get; private set; }
    public PermRedirectToRouteResult(RedirectToRouteResult redirect)
    {
      this.Redirect = redirect;
    }
    public override void ExecuteResult(ControllerContext context)
    {
      Redirect.ExecuteResult(context);
      context.HttpContext.Response.StatusCode = 301;
    }
  }
}


It turns out that the project containing the PermanentRedirectResult class still referenced the ASP.NET MVC 1.0 assembly, although the web site had been updated to reference 2.0. The framework tests the return value of the action method to see if it's an ActionResult or something else that should be wrapped in a ContentResult like this...

return ((actionReturnValue as ActionResult) ??
  new ContentResult {
    Content = Convert.ToString(actionReturnValue, CultureInfo.InvariantCulture)
  });

...and (actionReturnValue as ActionResult) is null because my PermanentRedirectResult extends the wrong ActionResult, the one in the 1.0 assembly rather than the one in the 2.0 assembly.

Updating the reference so that PermanentRedirectResult is a 2.0 ActionResult fixed the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜