Dipal Mehta
Dipal Mehta

Reputation: 462

Asp.net mvc weired behaviour in route data binding

Masters,

I am having a very simple problem with route data binding.

Please refer following code.

public class RemoveDashRouteHandler : MvcRouteHandler

{

    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        requestContext.RouteData.Values["controller"] = ((string)requestContext.RouteData.Values["controller"]).Replace("-", String.Empty);

        return base.GetHttpHandler(requestContext);
    }
}

And here what i am registering route

routes.MapRoute (

              name: "Categorywise",
              url: "{action}-{controller}/{nm}/{p}"
              ).RouteHandler = new RemoveDashRouteHandler();

Then i made a simple model like this

public class Params

{
    public string nm { get; set; }
    public string p { get; set; }
}

Now the problem is, when i hit URL : http://{mylocalip}:52730/Authority-cars/1/1 I always get all parameters null

And rewriting same with following works well.

public ActionResult Authority(string nm, string p)

    {
        ViewBag.ActionMethod = "Autority";

        return View("~/Views/Cars/Index.cshtml", new Params { nm = nm, p = p });
    }

What wrong i am doing. Please help.

Thanks in advance

Upvotes: 0

Views: 143

Answers (1)

Andy T
Andy T

Reputation: 9881

Based on how you have defined the route, there is no need to write a custom route handler to take into account the dash in the route.

routes.MapRoute(
                "Dash",                                              
                "{action}-{controller}/{nm}/{p}",                       
                new { controller = "Home", action = "Index" }
            );

Upvotes: 1

Related Questions