Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Migrate custom route to ASP.NET MVC custom route to support List<string>
- /market/prices/2011-05-01/min/stocks/msft/dell/appl/
- routes.MapRoute(
- "Stocks",
- "{queryDate}/{minOrMax}/stocks/{listOfStocksSeparatedByForwardSlash}",
- new { controller = "Market", action = "Prices" }
- );
- public ActionResult Prices(string queryDate, string minOrMax, ICollection<string> listOfStocksSeparatedByForwardSlash) {
- var model = repository.List(queryDate, minOrMax, listOfStocksSeparatedByForwardSlash);
- return View(model );
- }
- routes.MapRoute(
- "Stocks",
- "{queryDate}/{minOrMax}/stocks/{*listOfStocksSeparatedByForwardSlash}",
- new { controller = "Market", action = "Prices" }
- );
- public ActionResult Prices(string queryDate, string minOrMax, string listOfStocksSeparatedByForwardSlash) {
- var list = listOfStocksSeparatedByForwardSlash.Split('/').ToList();
- var model = repository.List(queryDate, minOrMax, list);
- return View(model );
- }
- routes.MapRoute(
- name: "Test",
- url: "Test/{someDate}/{*tickerSymbols}",
- defaults: new { controller = "Home", action = "Test" }).RouteHandler = new SlashSeparatedTrailingParametersRouteHandler("tickerSymbols", "tickers");
- public class SlashSeparatedTrailingParametersRouteHandler : IRouteHandler
- {
- private readonly string catchallParameterName;
- private readonly string actionTargetParameter;
- public SlashSeparatedTrailingParametersRouteHandler(string catchallParameterName, string actionTargetParameter)
- {
- this.catchallParameterName = catchallParameterName;
- this.actionTargetParameter = actionTargetParameter;
- }
- public IHttpHandler GetHttpHandler(RequestContext requestContext)
- {
- if (requestContext == null)
- {
- throw new ArgumentNullException("requestContext");
- }
- IRouteHandler handler = new MvcRouteHandler();
- var vals = requestContext.RouteData.Values;
- vals[this.actionTargetParameter] = vals[this.catchallParameterName].ToString().Split('/');
- return handler.GetHttpHandler(requestContext);
- }
- }
- [HttpGet]
- public ActionResult Test(DateTime someDate, string[] tickers)
- {
- if (tickers == null)
- {
- throw new ArgumentNullException("tickers");
- }
- return Content(string.Format("{0} and {1}", someDate, tickers.Length.ToString(CultureInfo.InvariantCulture)));
- }
- http://localhost/Test/12-06-2012/Foo/Bar
- 12/6/2012 12:00:00 AM and 2
Advertisement
Add Comment
Please, Sign In to add comment