0

当我的页面首次加载时,URL 如下所示:http://localhost:1245/。但是,我希望它看起来像这样:http://localhost:1245/Home/Index/2/2012?Events=Show(/2/2012?Events=Show 是重要的部分)。如何让我的页面从一开始就加载第二个 URL?

4

2 回答 2

2

我要做的是在默认 MVC 路由之前添加一个新路由,所以它看起来像:

routes.MapRoute(
  "NewDefault", // Route name
  "{controller}/{action}/{month}/{year}", // URL with parameters
  new { controller = "Events", 
        action = "Shows", 
        month = DateTime.Now.Month,
        year = DateTime.Now.Year } // Parameter defaults
  );

routes.MapRoute(
  "Default", // Route name
  "{controller}/{action}/{id}", // URL with parameters
  new { controller = "Home", 
        action = "Index", 
        id = UrlParameter.Optional } // Parameter defaults
  );

默认 URL(特别是对用户不可见)将是/Events/Shows/4/2012(如果日期是4/19/2012)。默认控制器是EventsController,默认操作是EventsController.Shows.

public EventsController
{
  public ActionResult Shows(int month, int year)
  {
    someModel model = new someModel();
    someModel.month = month;
    someModel.year = year;
    this.View(someModel);
  }
}
于 2012-04-19T22:17:56.097 回答
1

添加:

  routes.MapRoute("MyRouteName", 
    "Home/Index/2/2012", new { controller = "Home", action = "Index" });

到您的路线。

将 HomeController 的 Index 操作更改为具有以下签名:

public ActionResult Index(string Events)....
于 2012-04-19T19:25:47.130 回答