Botem Bao
Botem Bao

Reputation: 817

ASP NET Custom Routing

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?

Upvotes: 1

Views: 562

Answers (3)

Maxim Zaslavsky
Maxim Zaslavsky

Reputation: 18065

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.

Upvotes: 1

Bhushan Firake
Bhushan Firake

Reputation: 9458

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....

Upvotes: 1

cheesemacfly
cheesemacfly

Reputation: 11762

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();
    }
}

Upvotes: 1

Related Questions