0

I'm using asp.net MVC 3 for my website , I want to replace underlines with dashes in my addresses . I can do it . but when I want to replace them inside a area I can't do it .

who can help me ?

this is my code :

public class HyphenatedRouteHandler : MvcRouteHandler
        {
            protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
            {
                requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("-", "_");
                requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("-", "_");
                return base.GetHttpHandler(requestContext);
            }
        }

and this is my area route :

context.MapRoute(
                "products_default",
                "products/{controller}/{action}",
                new { controller = "All", action = "Index" }
            );

I want to navigate this address :

localhost:1559/products/store-builder/boronz

product is my area name . of course when I navigate this address :

localhost:1559/products/store_builder/boronz

it shows page .

EDIT :

I use this for my area route but it can't detect this is an area :

//context.Routes.Add(
//    new Route("products/{controller}/{action}",
//    new RouteValueDictionary(
//        new { controller = "", action = "Index" }),
//        new MyProject.MvcApplication.HyphenatedRouteHandler())
//);

how can I fix this ?

Persian.
  • 1,035
  • 3
  • 16
  • 34

2 Answers2

1

I think an easy option that you have is to adjust your route:

context.MapRoute(
    "products_default",
    "products/store-builder/{action}",
    new { controller = "YourControllerName", action = "Index" }
); 

The downside of this, though, is you will need to create a lot of routes as the number of controllers grows. Also, I may have answered a similar question here that creates a custom route handler like your sample starts out.

Community
  • 1
  • 1
hawkke
  • 4,242
  • 1
  • 27
  • 23
  • Yes this is easy option . but store builder is one of my products . website builder maybe is next . so I need to create another route for that . but I can use a pattern for all of them . – Persian. Oct 23 '12 at 17:31
0

You're calling string.replace wrong: the first parameter is the old value that will be replaced by the second parameter.

public class HyphenatedRouteHandler : MvcRouteHandler 
    { 
        protected override IHttpHandler GetHttpHandler(RequestContext requestContext) 
        { 
            requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("_", "-"); 
            requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("_", "-"); 
            return base.GetHttpHandler(requestContext); 
        } 
    } 
Destrictor
  • 752
  • 1
  • 4
  • 15
  • This works when I use this out of area . but in area it can't find address when I use dashes in address bar . – Persian. Oct 23 '12 at 09:35
  • @Persian. Unfortunately I don't understand what you mean by 'out of area', could you enlighten me a bit please? – Destrictor Oct 23 '12 at 09:38
  • I mean Areas . Products is an Area . within my project there is an Area with Products name . – Persian. Oct 23 '12 at 09:41
  • @Persian. I noticed you're not calling your custom routehandler when creating your area route: look [here](http://stackoverflow.com/questions/6457989/mvc3-routehandler-result-ignores-area-in-the-resolved-route-why) – Destrictor Oct 23 '12 at 09:49
  • I change it to : context.MapRoute( "products_default", "products/{controller}/{action}", new { controller = "All", action = "Index" }, new MyProject.MvcApplication.HyphenatedRouteHandler() ); – Persian. Oct 23 '12 at 11:03
  • Can you provide for me a sample ? – Persian. Oct 23 '12 at 12:00
  • @Persian. To be honest, i was just going on the obvious c# error, having no mvc experience whatsoever. If you want here's some more info on creating custom routehandlers: [geekswithblogs](http://geekswithblogs.net/sankarsan/archive/2009/01/18/developing-custom-routehandler.aspx) – Destrictor Oct 23 '12 at 12:15