You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ControllerExtensions.cs 866B

12345678910111213141516171819202122
  1. using System;
  2. using System.Linq.Expressions;
  3. using System.Web.Mvc;
  4. using System.Web.Routing;
  5. namespace Sdbarker.Extensions.RouteCollectionExtensions
  6. {
  7. public static class RouteRedirectExtensions
  8. {
  9. public static RedirectToRouteResult RedirectToRoute<TController>(this Controller controller, Expression<Func<TController, ActionResult>> action) where TController : IController
  10. {
  11. MethodCallExpression m = (MethodCallExpression)action.Body;
  12. if (m.Method.ReturnType != typeof(ActionResult))
  13. {
  14. throw new ArgumentException("ControllerAction method '" + m.Method.Name + "' does not return type ActionResult");
  15. }
  16. string name = typeof(TController).Name + m.Method.Name;
  17. RouteData r = (new RequestContext()).RouteData;
  18. return new RedirectToRouteResult(name, (r == null) ? new RouteValueDictionary() : r.Values);
  19. }
  20. }
  21. }