Joper
Joper

Reputation: 8209

ASP.NET MVC 3.0 Url Rewritng

I have Action called Contact in home controller

<mysite>/Home/Contact

I want to be able by typing <mysite>/Contact to get the same result as <mysite>/Home/Contact

Is it possible to do with mvc 3.0 routes or RouteMagic?

Currently i am trying to achieve this like that, but no luck:

Custom Routes:

routes.MapRoute(
                "Contact", // Route name
                "Contact", // URL with parameters
                new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
            );

RouteMagic:

 var route = routes.MapRoute("new", "Contact");
            routes.Redirect(r => r.MapRoute("old", "Home/Contact"))
              .To(route);

Update

Ok the custom routes should be defined first, now it is working(in case of custom routes), but there is appeared a new question why route magic returning error:

Server Error in '/' Application.
Value cannot be null or empty.
Parameter name: controllerName

Upvotes: 0

Views: 1897

Answers (2)

tvanfosson
tvanfosson

Reputation: 532465

Make sure your new route occurs before the default route (since it will match as well) when defining the route.

routes.MapRoute(
    "Contact", // Route name
    "contact", // URL with parameters
    new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
);

Upvotes: 1

Arrabi
Arrabi

Reputation: 3768

have you tried the rewrite module in iis7 ?

its easy to use , donwlod it from here:

http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/

Upvotes: 0

Related Questions