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?

4

3 回答 3

1

其他答案是有效的,但我认为有一种更简单、更清晰、更易于维护的方式来定义你的路由:使用基于属性的路由映射器

我发现最容易使用的是RiaLibrary.Web 路由映射器。您所要做的就是在您想要为其设置路线的每个方法上添加一个属性 - 然后指定您的路线详细信息。

要进行设置,您必须按照 RiaLibrary.Web 页面上列出的几个步骤进行操作。完成这些步骤后,您可以将任何控制器操作更改为如下所示:

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

如果您有任何可选参数,请将它们声明为{param?}在路由字符串中,然后int? param在您的方法声明中。

而已!您还可以包含一些其他参数来指定是否只有某些类型的 HTTP 调用匹配此路由,定义参数约束,并固定匹配此路由的顺序。

于 2012-12-16T06:45:11.190 回答
1

这是使您的控制器保持简单并且仍然具有良好 url 模式的答案:

控制器:

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

现在转到您的App_Start文件夹,打开RouteConfig.cs,开始在路由中添加 REST url,如下所示:

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

许多事情都被假定为数据类型和模型名称。你可以从中弄清楚它是如何工作的......

于 2012-12-16T05:02:21.277 回答
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();
    }
}
于 2012-12-15T15:19:53.173 回答