ASP ASP custom routing

I have the 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 prompt to be the controller, and the rest is the action. Each of the above URLs must correspond to a highlighted browse page.

Can someone help me devise routes?

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

I am updating the templates and adding my expectation at the end of each URL. Any suggestions on URL patterns?

+1
source share
3 answers

Here is the answer to keep your controller simple and still have nice 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 navigate to the folder App_Start, open RouteConfig.cs,, start adding REST URLs to routes, as shown 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") }
               );      

. , ...

+1

global.asax

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

- :

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

, , , : .

, , - RiaLibrary.Web route mapper. , , , , .

To configure, you must follow the several steps described on the RiaLibrary.Web page. After completing these steps, you can change any action of the controller in this way:

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

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

What is it! You can also include some other parameters to indicate whether only certain types of HTTP calls match this route, define restrictions on the parameters, and fix the order in which this route maps.

+1
source