Last active
January 11, 2016 18:15
-
-
Save jdaigle/453bcee73d8ee0ab7a7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq.Expressions; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using ExpressionHelper = Microsoft.Web.Mvc.Internal.ExpressionHelper; | |
namespace MyProject | |
{ | |
public static class UrlExtensions | |
{ | |
public static string Action<TController>(this UrlHelper url, Expression<Action<TController>> action) where TController : Controller | |
{ | |
if (url == null) | |
{ | |
throw new ArgumentNullException("url"); | |
} | |
RouteValueDictionary routeValues = ExpressionHelper.GetRouteValuesFromExpression(action); | |
return url.RouteUrl(routeValues); | |
} | |
} | |
public static class ControllerUrlExtensions | |
{ | |
public static RedirectToRouteResult RedirectToAction<TController>(this TController controller, Expression<Action<TController>> action, bool permanent = false) | |
where TController : Controller | |
{ | |
return RedirectToAction((Controller)controller, action, permanent); | |
} | |
public static RedirectToRouteResult RedirectToAction<TController>(this Controller controller, Expression<Action<TController>> action, bool permanent = false) | |
where TController : Controller | |
{ | |
if (controller == null) | |
{ | |
throw new ArgumentNullException("controller"); | |
} | |
RouteValueDictionary routeValues = ExpressionHelper.GetRouteValuesFromExpression(action); | |
return new RedirectToRouteResult("", routeValues, permanent); | |
} | |
public static string GetURL<TController>(this UrlHelper urlHelper, Expression<Action<TController>> action) | |
where TController : Controller | |
{ | |
return urlHelper.Action<TController>(action); | |
} | |
public static string GetURL<TController>(this TController controller, Expression<Action<TController>> action) | |
where TController : Controller | |
{ | |
return GetURL((Controller)controller, action); | |
} | |
public static string GetURL<TController>(this Controller controller, Expression<Action<TController>> action) | |
where TController : Controller | |
{ | |
if (controller == null) | |
{ | |
throw new ArgumentNullException("controller"); | |
} | |
return controller.Url.GetURL<TController>(action); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment