-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RouteMagic redirection giving 404 instead of redirecting #14
Comments
Did you confirm that there is no |
Yup. This was a brand new MVC project. |
My guess is that another route is matching the request first so you're not running into the redirect route. In the default MVC project, the default route matches everything. So the redirect route needs to go first. The following approach works in a brand new MVC 5 project. using System.Web.Mvc;
using System.Web.Routing;
using RouteMagic;
namespace RouteDemo
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Redirect(r => r.MapRoute("old", "Default.aspx"))
.To(routes.MapRoute("new", "Home/{action}", new { controller = "Home", action = "Index" }));
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
} For future reference, you can install the RouteDebugger package and it'll show you which routes are matching.
It's useful for figuring out these types of routing problems. |
Thanks for the info. I'll have a look. I don't remember where I put the default route, so it could be that it was was before the redirect route. Thanks also for the info about the package. I was wondering how you are supposed to debug routing! |
Hello,
I have rewritten an old WebForms site in MVC5 (Visual Studio 2013 Ultimate Update 4, Windows 7 64-bit), and want to add permanent redirection from the old URLs to the new. Reading your blog post about RouteMagic (http://haacked.com/archive/2011/02/02/redirecting-routes-to-maintain-persistent-urls.aspx/), I thought this would do the trick.
I added the NuGet package, and then added the following to the end of the RegisterRoutes() method in the RouteConfig class...
routes.Redirect(r => r.MapRoute("old", "Default.aspx")).To(routes.MapRoute("new", "Home"));
However, if I try to access http://localhost:8109/Default.aspx, I just get a 404 in the browser. It doesn't redirect at all.
What am I doing wrong? I reproduced the problem on a brand new, out-of-the box MVC project. I just added your package and the one line of code shown above.
Thanks for any help you can give.
The text was updated successfully, but these errors were encountered: