So I was going through some code of codebettercanvas and found it interesting of Karl Seguin using Html Helper extension for his form tags, which has its elegance. e.g
1 |
<form id="login" action="!{Html.LinkTo<HomeController>(c => c.Login()) }" method="post"> |
Basically it calls an extension method called LinkTo with a generic controller and a lamda for calling its action, the extension method looks like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public static class HtmlExtensions { private static readonly string _applicationPath = HttpContext.Current.Request.ApplicationPath == "/" ? string.Empty : HttpContext.Current.Request.ApplicationPath; //other method removed public static string LinkTo(this HtmlHelper html, Expression<Func<T, ActionResult>> action) { var body = action.Body as MethodCallExpression; if (body == null) { throw new InvalidOperationException("Expression must be a method call"); } var controller = typeof(T).Name.Replace("Controller", string.Empty); return string.Format("{0}/{1}/{2}", _applicationPath, controller, body.Method.Name); } } |
[…]