Hamid Reza
Hamid Reza

Reputation: 2973

How to make custom urls having the same controller method in ASP.Net MVC

I have these classes.

Secion Repository
Section Application
Section Controller
Group Repository
Group Application
Group Controller
Class Repository
Class Application
Class Controller

and all of my controllers have a view named Show And these structure of entities. enter image description here

Now the question is this: I want when I go to the Class controller I see

Class/Show/class's group's section name/class's group name/class's name

instead of

Class/Show/1

or when I go to the Group controller I see

Group/Show/group's section name/group name

instead of

Group/Show/1

How?

Upvotes: 0

Views: 71

Answers (1)

von v.
von v.

Reputation: 17118

You can define the following route:

routes.MapRoute(
    "ShowRoute",
    "{controller}/show/{groupsection}/{groupname}/{classname}",
    new { controller = "class", action = "show", classname= UrlParameter.Optional },                
);

A few things to note:

  1. You can choose any controller to be the default (e.g. controller="the_default")
  2. You can declare all parameters as optional but you need to take care of null arg values in your code

You can then define your controller methods like this:

public ActionResult Show(string groupsection, string groupname, string classname) {    
}

And then have the following requests:

http://your_domain/class/group1-section5/group1/class-obedient

http://your_domain/group/group1-section5/group1/

Upvotes: 1

Related Questions