在 MVC 5 中为日期创建自定义路由
Create Custom Route in MVC 5 for Date
我正在尝试转换以下 url
至
https://localhost:44322/BankHoliday/NewBankHoliday/holidayDate/08-25-2016/countryID/GBR
我已经尝试过此代码,但它不起作用
routes.MapRoute(
null,
"{holidayDate}/{countryID}",
new { Controller = "BankHoliday", action = "NewBankHoliday" }, new { holidayDate = @"\d{2}-\d{2}-\d{4}" }
);
要实现 BankHoliday/NewBankHoliday/holidayDate/08-25-2016/countryID/GBR
的 url,您的路线定义需要
routes.MapRoute(
name: "Bank",
url: "BankHoliday/NewBankHoliday/holidayDate/{holidayDate}/countryID/{countryID}",
defaults: new { controller = "BankHoliday", action = "NewBankHoliday"}
);
并且位于默认路由之前。不清楚为什么要在路线中使用 holidayDate
和 countryID
文本,更传统的 url 是
url: "BankHoliday/NewBankHoliday/{holidayDate}/{countryID}",
生成BankHoliday/NewBankHoliday/08-25-2016/GBR
那么控制器方法就需要
public class BankHolidayController : Controller
{
public ActionResult NewBankHoliday(DateTime holidayDate, string countryID)
{
....
假设您服务器上的文化接受 MM-dd-yyyy
格式的日期。
并在视图中生成 link
@Html.ActionLink("Your Link Text", "NewBankHoliday", "BankHoliday", new { holidayDate = "08-25-2016", countryID = "GBD"}, null)
我正在尝试转换以下 url
至 https://localhost:44322/BankHoliday/NewBankHoliday/holidayDate/08-25-2016/countryID/GBR
我已经尝试过此代码,但它不起作用
routes.MapRoute(
null,
"{holidayDate}/{countryID}",
new { Controller = "BankHoliday", action = "NewBankHoliday" }, new { holidayDate = @"\d{2}-\d{2}-\d{4}" }
);
要实现 BankHoliday/NewBankHoliday/holidayDate/08-25-2016/countryID/GBR
的 url,您的路线定义需要
routes.MapRoute(
name: "Bank",
url: "BankHoliday/NewBankHoliday/holidayDate/{holidayDate}/countryID/{countryID}",
defaults: new { controller = "BankHoliday", action = "NewBankHoliday"}
);
并且位于默认路由之前。不清楚为什么要在路线中使用 holidayDate
和 countryID
文本,更传统的 url 是
url: "BankHoliday/NewBankHoliday/{holidayDate}/{countryID}",
生成BankHoliday/NewBankHoliday/08-25-2016/GBR
那么控制器方法就需要
public class BankHolidayController : Controller
{
public ActionResult NewBankHoliday(DateTime holidayDate, string countryID)
{
....
假设您服务器上的文化接受 MM-dd-yyyy
格式的日期。
并在视图中生成 link
@Html.ActionLink("Your Link Text", "NewBankHoliday", "BankHoliday", new { holidayDate = "08-25-2016", countryID = "GBD"}, null)