Monday, March 24, 2014

Don't use Response.Redirect in an ASP.NET MVC Controller Action. It won't work.

This is bad:

using System.Web.Mvc;
using FluffyNothing.Core.Objects;
namespace FluffyNothing.UserInterface.Controllers
{
   public class AccountController : MvcController
   {
      public ActionResult Index()
      {
         Person person = GetFullIdentity();
         if (person == null)
         {
            Response.Redirect("/");
         }
         return View(person);
      }
   }
}

 
 

This is better:

using System.Web.Mvc;
using FluffyNothing.Core.Objects;
namespace FluffyNothing.UserInterface.Controllers
{
   public class AccountController : MvcController
   {
      public ActionResult Index()
      {
         Person person = GetFullIdentity();
         if (person == null)
         {
            return RedirectToAction("Index", "Home");
         }
         return View(person);
      }
   }
}

No comments:

Post a Comment