1

I have following patterns

/invitation/mission/busstop  -- return the list of busstops
/invitation/mission/busstop/id  -- return a specific busstop
/invitation/mission/driver  -- return the list of drivers
/invitation/mission/driver/id  -- return a specific driver
/invitation/mission/driver/city/model/limit  -- query driver accoring to city, model and age limit
...
/invitation/questionair  -- return the list of questionairs
/invitation/questionair/id  -- return a specific questionair
/invitation/questionair/create  -- create a new questionair
/invitation/questionair/update/id  -- update a questionair
...

I expect the 'invitation' to be controller, and the rest to be action. Each of the above url should corresponds to a dedicated view page.

Can anyone help me to design the routes?

=====================================================================

I update the patterns, and add my expectation at the end of each url. Any suggest on the url patterns?

2
  • Do you want to use RESTFul urls? What do you want to do with the last sample you have shown? Should there be any questionid or something? Commented Dec 15, 2012 at 15:13
  • Bhushan, I updated my question a little bit. Yes, you are right. The urls should be REST style as we may build Apps running on the mobile device as well. So far, I have no idea of what the urls should looks like. Thank you. Commented Dec 15, 2012 at 22:42

3 Answers 3

1

Here is the answer to keep your controller simple and still having good url patterns:

Controller:

 public class InvitationController : Controller
    {
        public ActionResult GetAllBusStops()
        {
            //Logic to show all bus stops
            //return bus stops 
            return View();
        }

        public ActionResult GetBusStopById(string id)  //Assumed your id to be a string
        {
            //Logic to get specific bus stop
            //return bus stop

            return View();
        }

        public ActionResult GetAllDrivers()
        {
            //Logic for driver list
            //return driver list 
            return View();
        }

        public ActionResult GetDriverById(int id)  //Assumed your id to be an integer
        {
            //Logic to get specific driver
            //return driver

            return View();
        }
        public ActionResult GetDriver(string city, string model,int limit)  //Assumed datatypes 
        {
            //Logic to get specific driver with this criteria
            //return driver

            return View();
        }
         public ActionResult GetAllQuestionairs()
        {
            //Logic for questionair list
            //return the list 
            return View();
        }
        public ActionResult GetQuestionairById(int id)  //Assumed your id to be an integer
        {
            //Logic to get specific questionair
            //return it

            return View();
        }
        public ActionResult CreateQuestionair(QuestionairCreateModel model)
        {
            //logic to create questionair
            return View();

        }
        public ActionResult GetQuestionairById(int id)  //Assumed your id to be an integer
        {
            //Logic to get specific questionair
            //return it

            return View();
        }

        public ActionResult UpdateQuestionairById(int id)  //Assumed your id to be an integer
        {
            //Logic to update specific questionair
            //return it

            return View();
        }
    }

Now go to your App_Start Folder, Open RouteConfig.cs, start adding the REST urls in the routes as below:

routes.MapRoute(
                "List Bus Stops", // Route name
                "invitation/mission/busstop", // No parameters for Getting bus stop list
                new { controller = "Invitation", action = "GetAllBusStops"}, // Parameter defaults
                new { httpMethod = new HttpMethodConstraint("GET") }
                );

            routes.MapRoute(
                "Get Bus stop by id", // Route name
                "invitation/mission/busstop/{id}", // URL with parameters
                new { controller = "Invitation", action = "GetBusStopById" }, // Parameter defaults
                new { httpMethod = new HttpMethodConstraint("GET") }
                );
             routes.MapRoute(
                "Get All Drivers", // Route name
                "invitation/mission/driver", // No parameters for Getting driver list
                new { controller = "Invitation", action = "GetAllDrivers"}, // Parameter defaults
                new { httpMethod = new HttpMethodConstraint("GET") }
                );
            routes.MapRoute(
                "Get Driver by id", // Route name
                "invitation/mission/driver/{id}", // URL with parameters
                new { controller = "Invitation", action = "GetDriverById" }, // Parameter defaults
                new { httpMethod = new HttpMethodConstraint("GET") }
                );

            routes.MapRoute(
                "Get driver for city, model, limit", // Route name
                "invitation/mission/driver/{city}}/{model}/{limit}", // URL with parameters
                new { controller = "invitation", action = "GetDriver"}, // Parameter defaults
                new { httpMethod = new HttpMethodConstraint("GET") }
                );
            routes.MapRoute(
                "Get All Questionairs", // Route name
                "invitation/questionair", // No parameters for Getting questionair list
                new { controller = "Invitation", action = "GetAllQuestionairs"}, // Parameter defaults
                new { httpMethod = new HttpMethodConstraint("GET") }
                );
             routes.MapRoute(
                "Get questionair by id", // Route name
                "invitation/questionair/{id}", // URL with parameters
                new { controller = "Invitation", action = "GetQuestionairById" }, // Parameter defaults
                new { httpMethod = new HttpMethodConstraint("GET") }
                );
            routes.MapRoute(
               "Create New Questionair, // Route name
               "invitation/questionair/create", // URL with parameters
               new { controller = "invitation", action = "CreateQuestionair" }, // Parameter defaults
               new { httpMethod = new HttpMethodConstraint("POST") }
               );   

            routes.MapRoute(
               "Update Questionair, // Route name
               "invitation/questionair/update/{id}", // URL with parameters
               new { controller = "invitation", action = "UpdateQuestionairById" }, // Parameter defaults
               new { httpMethod = new HttpMethodConstraint("POST") }
               );      

Many things are assumed like the datatypes and model names. You can figure out how it works from this....

Sign up to request clarification or add additional context in comments.

5 Comments

Bhushan, thank you so much! I got one more question about QuestionairCreateModel. Is there another one - QuestionairUpdateModel for update? I'm curious about why not QuestionairModel which can be used either in CreateQuestionair or in UpdateQuestionair?
yeah, you can keep an UpdateQuestionairModel..it will be much better if you do so..it will be clear to yourself while going through the code afterwards..
Thank you. You definitely answered my question. Thanks again.
@BhushanFirake sir, what if I have a route [GET] api/classes/{classID}/coaches, which represent the coaches who run the class with id=classID, but what if i want to see(GET) all the coaches, should i have different controller or should it be under different controller? what should be the route? I am using API using .net core 5.
@GKhedekar I would create another controller for coaches with GET api/coaches endpoint
1

Add the route in the global.asax

routes.MapRoute(
   "Default",
   "{controller}/{id}",
   new { controller = "invitation", action = "Index" }
);

Then in your controller use something like:

public class InvitationController : Controller
{
    public ActionResult Index(string id)
    {
        switch(id.ToLower())
        {
            case "mission/busstop/list":
                return View("busstop/list");
            case "mission/driver/query":
                return View("driver/query");
        }
        //Return default view    
        return View();
    }
}

1 Comment

Thank you. Is there any other way instead of putting a lot of stuff here to do 'routing' mannually. I want a way to extend '/controller/action' so I can build versatile patterns but still keep the Controller simple and clear.
1

The other answers are valid, but I think there is a much easier, cleaner, and more maintainable way of defining your routes: use an attribute-based route mapper.

The easiest one I've found to use is the RiaLibrary.Web route mapper. All you have to do is add an attribute on each method you want to have a route for - then specify your route details.

To get set up, you must follow a few steps outlined on the RiaLibrary.Web page. After completing those steps, you can change any Controller action to look like this:

[Url("Articles/{id}")]
public ActionResult Details(int id)
{
    // process
    return View();
}

If you have any optional parameters, declare them as {param?} in the route string and then as int? param in your method declaration.

That's it! You can also include some other parameters to specify whether only certain types of HTTP calls match this route, to define constraints on parameters, and to fix the order in which this route is matched.

2 Comments

Side note: in the next few weeks I will be publishing an open source ASP.NET MVC template that will include examples of many different routing scenarios and also ways to debug route mapping. Let me know if you're interested and I'll keep you posted.
That is really cool. Looking forward your MVC template. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.