views:

113

answers:

5

How can I rewrite a url like: /SomePage to /Pages/ShowPage/SomePage?

I tried:

            routes.MapRoute("myroute", "{id}", new { controller = "Pages", action = "ShowPage" });

But It's not working. What am I doing wrong?

A: 

I think this should be:

routes.MapRoute("myroute", "{controller}/{action}/{id}", new { controller = "Pages", action = "ShowPage", id = "SomePage" });
pmarflee
still dosen't working.
TTT
A: 

It was wrong because I think in your application, there is this default map route :

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

it will looks for the controller with the name equal to the id that you passed in, if you remove this default map route, your map route would work.

You should try this route debugger tool, it helps out a lot:

Debugger Tool

tuanvt
A: 

You need to ensure that your route maps to your objects. In your case, you need to have a controller called PagesController with a method called ShowPage, with a single parameter called pagename (if you use a route like the following).

routes.MapRoute("route", "{controller}/{action}/{pagename}", new { controller = "Pages", action = "ShowPage", pagename = "" }  );

Also, do not forget that you can use regex when specifying the route - this may help you ensure the correct route is used by the routing engine.

routes.Add(new Route("{controller}/{action}/{params}",
    new RouteValueDictionary { { "controller", "user" }, { "action", "login" }, { "params", "" } },
    new RouteValueDictionary { { "controller", @"^(?!Resources)\w*$" }, { "action", "[a-zA-Z]+" } },
    new MvcRouteHandler()));
Mike
A: 

You can write your own static routing. Above the default route add your own.

routes.MapRoute("MyRoute",                                              // Route name            
    "Pages/ShowPage/SomePage/{id}",                           // URL with parameters            
    new { controller = "Pages", action = "ShowPage", id = "" }  // Parameter defaults        
);

Now, if SomePage is a variable, you'll want something like this:

routes.MapRoute("MyRoute",                                              // Route name            
    "Pages/ShowPage/{somePage}/{id}",                           // URL with parameters            
    new { controller = "Pages", action = "ShowPage", id = "", somePage = "" }  // Parameter defaults        
);

You can leave out the {id} if you want, just leave it out of your action parameters.

routes.MapRoute("MyRoute",                                              // Route name            
    "Pages/ShowPage/{somePage}",                           // URL with parameters            
    new { controller = "Pages", action = "ShowPage", somePage = "" }  // Parameter defaults        
);
Josh
+1  A: 

If you are trying to say "navigating to /SomePage shall call PagesController.ShowPage("SomePage")", then you probably want this:

// Find a method with signature PagesController.ShowPage( string param )
// and call it as PagesController.ShowPage("SomePage")
route.MapRoute(
    "MyRoute",
    "SomePage",
    new { controller = "Pages", action = "ShowPage", param = "SomePage" } );

This will only redirect the exact URL /SomePage. If you are trying to say "navigating to /{something} shall run the PagesController.ShowPage( something ) method", then that is a more difficult problem.

If this second case is indeed what you want, then you'll have to define it after most of your other routes. The routing entry you would want would be:

// This will call the method PagesController.ShowPage( string param )
route.MapRoute(
    "MyRoute",
    "{param}",
    new { controller = "Pages", action = "ShowPage" } );
Stephen Jennings