Guest User

Untitled

a guest
Aug 2nd, 2012
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. Migrate custom route to ASP.NET MVC custom route to support List<string>
  2. /market/prices/2011-05-01/min/stocks/msft/dell/appl/
  3.  
  4. routes.MapRoute(
  5. "Stocks",
  6. "{queryDate}/{minOrMax}/stocks/{listOfStocksSeparatedByForwardSlash}",
  7. new { controller = "Market", action = "Prices" }
  8. );
  9.  
  10. public ActionResult Prices(string queryDate, string minOrMax, ICollection<string> listOfStocksSeparatedByForwardSlash) {
  11. var model = repository.List(queryDate, minOrMax, listOfStocksSeparatedByForwardSlash);
  12. return View(model );
  13. }
  14.  
  15. routes.MapRoute(
  16. "Stocks",
  17. "{queryDate}/{minOrMax}/stocks/{*listOfStocksSeparatedByForwardSlash}",
  18. new { controller = "Market", action = "Prices" }
  19. );
  20.  
  21. public ActionResult Prices(string queryDate, string minOrMax, string listOfStocksSeparatedByForwardSlash) {
  22. var list = listOfStocksSeparatedByForwardSlash.Split('/').ToList();
  23. var model = repository.List(queryDate, minOrMax, list);
  24. return View(model );
  25. }
  26.  
  27. routes.MapRoute(
  28. name: "Test",
  29. url: "Test/{someDate}/{*tickerSymbols}",
  30. defaults: new { controller = "Home", action = "Test" }).RouteHandler = new SlashSeparatedTrailingParametersRouteHandler("tickerSymbols", "tickers");
  31.  
  32. public class SlashSeparatedTrailingParametersRouteHandler : IRouteHandler
  33. {
  34. private readonly string catchallParameterName;
  35. private readonly string actionTargetParameter;
  36.  
  37. public SlashSeparatedTrailingParametersRouteHandler(string catchallParameterName, string actionTargetParameter)
  38. {
  39. this.catchallParameterName = catchallParameterName;
  40. this.actionTargetParameter = actionTargetParameter;
  41. }
  42.  
  43. public IHttpHandler GetHttpHandler(RequestContext requestContext)
  44. {
  45. if (requestContext == null)
  46. {
  47. throw new ArgumentNullException("requestContext");
  48. }
  49.  
  50. IRouteHandler handler = new MvcRouteHandler();
  51. var vals = requestContext.RouteData.Values;
  52. vals[this.actionTargetParameter] = vals[this.catchallParameterName].ToString().Split('/');
  53. return handler.GetHttpHandler(requestContext);
  54. }
  55. }
  56.  
  57. [HttpGet]
  58. public ActionResult Test(DateTime someDate, string[] tickers)
  59. {
  60. if (tickers == null)
  61. {
  62. throw new ArgumentNullException("tickers");
  63. }
  64.  
  65. return Content(string.Format("{0} and {1}", someDate, tickers.Length.ToString(CultureInfo.InvariantCulture)));
  66. }
  67.  
  68. http://localhost/Test/12-06-2012/Foo/Bar
  69.  
  70. 12/6/2012 12:00:00 AM and 2
Advertisement
Add Comment
Please, Sign In to add comment