C# 在MVC 5中为日期创建自定义管线
我正在尝试转换下面的url 到 我已经尝试过这个代码,但它不起作用C# 在MVC 5中为日期创建自定义管线,c#,asp.net-mvc,asp.net-mvc-routing,C#,Asp.net Mvc,Asp.net Mvc Routing,我正在尝试转换下面的url 到 我已经尝试过这个代码,但它不起作用 routes.MapRoute( null, "{holidayDate}/{countryID}", new { Controller = "BankHoliday", action = "NewBankHoliday" }, new { holidayDate = @"\d{2}-\d{2}-\d{4}" }
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"}
);
public class BankHolidayController : Controller
{
public ActionResult NewBankHoliday(DateTime holidayDate, string countryID)
{
....
并位于默认路线之前。现在还不清楚为什么要在路线中使用文本holidayDate
和countryID
,而更传统的url是
url: "BankHoliday/NewBankHoliday/{holidayDate}/{countryID}",
生成BankHoliday/NewBankHoliday/08-25-2016/GBR
那么控制器方法将需要
routes.MapRoute(
name: "Bank",
url: "BankHoliday/NewBankHoliday/holidayDate/{holidayDate}/countryID/{countryID}",
defaults: new { controller = "BankHoliday", action = "NewBankHoliday"}
);
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)
要获得
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"}
);
public class BankHolidayController : Controller
{
public ActionResult NewBankHoliday(DateTime holidayDate, string countryID)
{
....
并位于默认路线之前。现在还不清楚为什么要在路线中使用文本holidayDate
和countryID
,而更传统的url是
url: "BankHoliday/NewBankHoliday/{holidayDate}/{countryID}",
生成BankHoliday/NewBankHoliday/08-25-2016/GBR
那么控制器方法将需要
routes.MapRoute(
name: "Bank",
url: "BankHoliday/NewBankHoliday/holidayDate/{holidayDate}/countryID/{countryID}",
defaults: new { controller = "BankHoliday", action = "NewBankHoliday"}
);
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)
如何生成url(使用
Html.ActionLink()
)?我使用@Html。ActionLink@Html.ActionLink(“我的数据”、“NewBankHoliday”、新的{holidayDate=item.BankHolidayDate、countryID=item.countryID}、新的{@class=“glyphicon glyphicon trash”})您是如何生成url的(使用Html.ActionLink()
)?我正在使用@Html。ActionLink@Html.ActionLink(“我的数据”,“NewBankHoliday”,新的{holidayDate=item.BankHolidayDate,countryID=item.countryID},新的{@class=“glyphicon glyphicon trash”})我正在使用@Html。ActionLink@Html.ActionLink(“我的数据”,“NewBankHoliday”,新建{holidayDate=item.BankHolidayDate,countryID=item.countryID},新建{@class=“glyphicon glyphicon trash”})我在上收到一个异常。从客户端()检测到一个潜在危险的请求。路径值@maxspan-您需要确保holidayDate
包含带破折号的字符串格式的日期,而不是斜杠。因此您的服务器不支持该日期格式。在这种情况下,您需要使用new{holidayDate=item.bankhidaydate.ToString(“yyyy-MM-dd”),countryID=item.countryID}
尽管这样会产生../BankHoliday/NewBankHoliday/2016-08-25/GBR
(即ISO格式),但仍然会正确绑定到日期时间
参数您不能有2个空
参数并使用路由定义(只有最后一个参数可以是可选的)。如果您只提供了其中一个值,则它将再次使用查询字符串值。是否有任何原因不希望同时传递这两个值?我使用的是@Html。ActionLink@Html.ActionLink(“我的数据”,“NewBankHoliday”,新的{holidayDate=item.BankHolidayDate,countryID=item.countryID},新的{@class=“glyphicon glyphicon trash”})我的上出现异常。从客户端()检测到一个潜在危险的请求。路径值@maxspan-您需要确保holidayDate
包含带破折号的字符串格式的日期,而不是斜杠。因此您的服务器不支持该日期格式。在这种情况下,您需要使用new{holidayDate=item.bankhidaydate.ToString(“yyyy-MM-dd”),countryID=item.countryID}
尽管这样会产生../BankHoliday/NewBankHoliday/2016-08-25/GBR
(即ISO格式),但仍然会正确绑定到日期时间
参数您不能有2个空
参数并使用路由定义(只有最后一个参数可以是可选的)。如果只提供了其中一个值,则它将再次使用查询字符串值。是否有任何原因不希望同时传递这两个值?