2

Here is my route in Global.asax to remove /Home:

    routes.MapRoute("Root", "{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
   );

Well I need to setup a 301 redirect because someone linked to /Home and they're getting a 404.

So how do I setup the 301?

I checked the way the route was setting up and it is looking for a "Home" action method in the "Home" controller.

So obviously I could add:

public ActionResult Home() {
    Response.Status = "301 Moved Permanently";
    Response.RedirectLocation = "/";
    Response.End();
    return Redirect("~/");
}

However, there's gotta be a better way of doing this right?

Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117

1 Answers1

8

If you want to allow this URL, you can do

routes.MapRoute("Root", "Home",
     new { controller = "Home", action = "Index", id = UrlParameter.Optional });

But you want redirection, and it does make most sense, so...

Another thing you can do is create another controller Redirector and an action Home.

public class RedirectorController : Controller
{
    public ActionResult Home()
    {
        return RedirectPermanent("~/");
    }
}

Then you set the routes as:

routes.MapRoute("Root", "Home",
        new { controller = "Redirector", action = "Home"});

Remember to add the route at the top of your routes so that the generic routes don't match instead.

Update:

Another thing you can do is add this to the end of your routes:

routes.MapRoute("Root", "{controller}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional });

But this is not redirect still. So, can change the Redirector to be generic as...

public class RedirectorController : Controller
{
    public ActionResult Redirect(string controllerName, string actionName)
    {
        return RedirectToActionPermanent(actionName, controllerName);
    }
}

Then the route (which should be now at the bottom of all routes) will be:

routes.MapRoute("Root", "{controllerName}",
        new { controller = "Redirector", action = "Redirect", 
              controllerName = "Home", actionName = "Index" });

So, it'll try to redirect to the Index action of a controller with the same name as /name. Obvious limitation is the name of the action and passing parameters. You can start building on top of it.

Meligy
  • 35,654
  • 11
  • 85
  • 109
  • Too funny, I was editing my "question" right as you typed this and came up with the same thing. – Jack Marchetti Feb 22 '11 at 00:56
  • Any idea why links to Url.Action("Index", "Home") would now appear like: http://example.com/Home/Index?action=Index&controller=Home – Jack Marchetti Feb 22 '11 at 04:07
  • If after taking the last path in the update, are you sure you added it at the very bottom? – Meligy Feb 22 '11 at 04:10
  • I fixed it. I also found that just making a link to – Jack Marchetti Feb 22 '11 at 04:52
  • Aha. I thought you already had that and the link your are talking about is out of your hand (like, already indexed in search engines or likely bookmarked by users or so). – Meligy Feb 22 '11 at 05:04
  • 1
    Make sure to use double quotes on the controllerName var otherwise it will not build. routes.MapRoute("Root", "{controllerName}", new { controller = "Redirector", action = "Redirect", controllerName = "Home", actionName = "Index" }); – jamierushad Nov 02 '15 at 17:43