Hamid Reza
Hamid Reza

Reputation: 2973

How to achieve this url routing?

Let me explain briefly what I need:

I have these controllers:

And these actions:

Now the urls are like the following:

I want to write a url routing to achieve this:

How to do this?

Thanks in advanced.

Upvotes: 0

Views: 62

Answers (2)

Vedat Taylan
Vedat Taylan

Reputation: 136

RouteConfig

routes.MapRoute(
    name: "Detail",
    url: "{controller}/{action}/{id}/{name}",
    defaults: new { 
        controller = "User", 
        action = "Show", 
        id = UrlParameter.Optional, 
        name = UrlParameter.Optional 
    }
);

Code Behind

Public ActionResult Show(int id)
{

}

Upvotes: 1

Roy Dictus
Roy Dictus

Reputation: 33139

All you need to do is modify the signatures of your Controller methods, like so:

In your UserController:

public ViewResult Show(string id)

In your ProductController:

public ViewResult Show(string category)

and in your CommentController:

public ViewResult Show(int id)

and it will work (of course you need to change the implementation too... this is assuming, for instance, that the string "hamid-reza" is an ID of some kind).

Upvotes: 1

Related Questions