如何格式化MVC Route参数?

jam*_*ind 5 routes asp.net-mvc-3

我在我的测试MVC3项目中映射了以下路由 -

routes.MapRoute(
    "TestRoute",
    "test/{DateFrom}/{DateTo}",
    new { controller = "Home", action = "TestRoute" }
);
Run Code Online (Sandbox Code Playgroud)

然后我在视图中建立一个链接如下 -

@Html.ActionLink("Test Link", "TestRoute", new
{
    DateFrom = new DateTime(2006, 02, 16),
    DateTo = new DateTime(2008, 04, 22)
})
Run Code Online (Sandbox Code Playgroud)

在渲染时,输出此URL -

/test/02/16/2006%2000%3a00%3a00/04/22/2008%2000%3a00%3a00
Run Code Online (Sandbox Code Playgroud)

如您所见,框架调用ToString()DateTime参数,然后对结果进行了编码.

我想格式化DateTime路由参数,以便它们输出为"yyyy-MM-dd".

显然我可以在构建Action Link时指定格式,如下所示 -

@Html.ActionLink("Test Link", "TestRoute", new
{
    DateFrom = new DateTime(2006, 02, 16).ToString("yyyy-MM-dd"),
    DateTo = new DateTime(2008, 04, 22).ToString("yyyy-MM-dd")
})
Run Code Online (Sandbox Code Playgroud)

但是我们知道这很麻烦,很麻烦而且不干.

我已经尝试[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")]在模型中的属性上使用属性,希望这是答案,但是看起来这些只在构建编辑器表单时才受到尊重.

仅供参考,如果我DateTime在浏览器中手动输入URL中的参数(例如/ test/2006-02-16/2008-04-22),参数会被正确解析,因此只需要按照我想要的格式对它们进行格式化.

Zac*_*een 4

您可以编写一个自定义帮助程序来处理具有日期的链接:

namespace System.Web.Mvc {
    public static class LinkExtensions {
        public static MvcHtmlString DateLink(this HtmlHelper htmlHelper, string linkText, string actionName) {
            return DateLink(htmlHelper, linkText, actionName, new RouteValueDictionary(), null);
        }
        public static MvcHtmlString DateLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues) {
            return DateLink(htmlHelper, linkText, actionName, new RouteValueDictionary(routeValues), new RouteValueDictionary());
        }
        public static MvcHtmlString DateLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes) {
            return DateLink(htmlHelper, linkText, actionName, new RouteValueDictionary(routeValues), HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
        public static MvcHtmlString DateLink(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues) {
            return DateLink(htmlHelper, linkText, actionName, routeValues, new RouteValueDictionary());
        }
        public static MvcHtmlString DateLink(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes) {
            if (String.IsNullOrEmpty(linkText)) {
                throw new ArgumentException("linkText");
            }


            //check additional parameters
            List<string> keys = new List<string>(routeValues.Keys);
            foreach (string key in keys) {
                 if (routeValues[key].GetType() == typeof(DateTime))
                      routeValues[key] = ((DateTime)routeValues[key]).ToString("yyyy-MM-dd");
            }

            return MvcHtmlString.Create(HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext,
                htmlHelper.RouteCollection, linkText, null, actionName, (string)routeValues["controller"], routeValues, htmlAttributes));
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用它:

@Html.DateLink("TestLink", "Details", new { starttime = new DateTime(2011, 1, 1), endtime = new DateTime(2012, 1, 1)})
Run Code Online (Sandbox Code Playgroud)

生成以下 URL:

http://localhost/MyController/Details/2011-01-01/2012-01-01
Run Code Online (Sandbox Code Playgroud)