我正在尝试转换以下网址
要 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}" }
);
答案 0 :(得分:2)
要获得BankHoliday/NewBankHoliday/holidayDate/08-25-2016/countryID/GBR
的网址,您的路由定义需要
routes.MapRoute(
name: "Bank",
url: "BankHoliday/NewBankHoliday/holidayDate/{holidayDate}/countryID/{countryID}",
defaults: new { controller = "BankHoliday", action = "NewBankHoliday"}
);
并位于默认路线之前。不清楚为什么你想在路线中找到文本holidayDate
和countryID
,而更传统的网址是
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
格式的日期。
并在视图中生成链接
@Html.ActionLink("Your Link Text", "NewBankHoliday", "BankHoliday", new { holidayDate = "08-25-2016", countryID = "GBD"}, null)