Tutorial :In ASP.NET MVC, how can I rewrite from /somepage to /pages/showpage/somepage?



Question:

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?


Solution:1

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" } );  


Solution:2

I think this should be:

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


Solution:3

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


Solution:4

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()));  


Solution:5

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          );  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »