Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Omit controller name from URL mvc

I'm looking to do something similar to this post:

How to hide controller name in Url?

only without any sort of ID.

The server is running IIS 6 and the pages already show up without extensions so it's not a wildcard issue.

I'm looking to hit http://website.com/action-name

I have http://website.com/controller/action-name working

I'm assuming this is just a simple routing change that I am somehow goofing up. My current routing rule is:

routes.MapRoute(
    "RouteName",
"{action}",
new { controller = "Home", action = "Index" }
);
like image 341
mwright Avatar asked Jul 02 '10 21:07

mwright


2 Answers

Is your new routing rule positioned above the default routing rule of {controller, action, id} so that it has the opportunity to match first?

like image 63
Robert Harvey Avatar answered Oct 27 '22 03:10

Robert Harvey


The problem is your default route is still probably in place so it is matching it first and defaulting the rest of the inputs it expects. Based on your comment that the controller/action is working makes me think you didn't remove it or it is appearing first. Can you post your entire RegisterRoutes?

Try making the route you defined the very first route and it should match almost anything you pass at it.

EDIT: Added what your RegisterRoutes should look like:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // This will match anything so if you have something very specific with hard coded
    // values or more items that will need to be match add them here above but do not
    // add defaulted values so it can still fall through to this.
    routes.MapRoute( 
        "RouteName", 
        "{action}", 
        new { controller = "Home", action = "Index" });
}
like image 38
Kelsey Avatar answered Oct 27 '22 02:10

Kelsey