How to control ASP.NET MVC Route generation?
I have a route like
routes.MapRoute(
"Reports", // Route name.
"repor开发者_如何转开发ts/{controller}/{action}/{id}/{start}/{end}", // URL with parameters.
new { controller = "Home", action = "Index", id = "", start="", end="" } // Parameter defaults.
);
where start
and end
are DateTime?
When I write an Html.ActionLink, it generates the route using the DateTime.ToString
method. Those dates have forward slashes in them which disrupts the route. I'd like to somehow control the route generation to generate ISO 8601 date strings.
Ideally I don't want to have to change my Actions to take strings and convert in my code, I'd like to signal the route generator to call the appropriate ToString
overload of my choice.
In your Html.ActionLink
, when you're declaring the start and end values, why not do something like:
new{start = String.Format("{0:YYYY-MM-dd}", SomeDate)}
or this if you need the time in there too:
new{start = String.Format("{0:s}", SomeDate)}
Bit of work, but it will guarentee it's formatted correctly.
精彩评论