MapRoute with my extension

372 views Asked by At

How to create right MapRoute with my extension? (e.x. "my"). This MapRoute is correct

    routes.MapRoute(
        name: "Default",
        url: "{controller}.my/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

But I want use controller.my?id. This MapRoute is wrong

routes.MapRoute(
    name: "",
    url: "{controller}.my/{tid}",
    defaults: new { controller = "Home", action = "Index", tid = UrlParameter.Optional }
);
1

There are 1 answers

3
Amin Saqi On

I think that if you add parameters in MapRoute, you can't simply use them as query strings.

Remove those parameters which you want to use them as query strings from MapRoute, and simply use them in action methods:

routes.MapRoute(
    name: "",
    url: "{controller}.my",
    defaults: new { controller = "Home", action = "Index" }
);

Action method:

public ActionResult Index(string tid) // or int tid ...
{
    // ...
}

So you can access that by something like ~/Home.my?tid=someThing