Last active
December 14, 2015 10:18
-
-
Save jasonmitchell/5070743 to your computer and use it in GitHub Desktop.
Associated blog article http://jason-mitchell.com/asp-net-programming/t4-templating-to-generate-code-for-asp-net-mvc-actions/
This file contains 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
<#@ template debug="true" hostSpecific="true" #> | |
<#@ output extension=".cs" #> | |
<#@ Assembly Name="System.Core" #> | |
<#@ Assembly Name="System.Windows.Forms" #> | |
<#@ Assembly Name="System.Web.Mvc" #> | |
<#@ assembly name="$(SolutionDir)MyProject.WebApplication\bin\MyProject.WebApplication.dll" #> | |
<#@ import namespace="System" #> | |
<#@ import namespace="System.IO" #> | |
<#@ import namespace="System.Diagnostics" #> | |
<#@ import namespace="System.Linq" #> | |
<#@ import namespace="System.Collections" #> | |
<#@ import namespace="System.Collections.Generic" #> | |
<#@ import namespace="System.Reflection" #> | |
<#@ import namespace="System.Web.Mvc" #> | |
<#@ import namespace="Hermes.WebApplication.Controllers" #> | |
<# | |
Type[] controllerTypes = Assembly.GetAssembly(typeof(AccountController)).GetTypes().Where(t => String.Equals(t.Namespace, "MyProject.WebApplication.Controllers", StringComparison.Ordinal)).ToArray(); | |
var actionMethods = controllerTypes.SelectMany(x => x.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)); | |
var actionDefinitions = from method in actionMethods | |
where method.CustomAttributes.All(x => x.AttributeType != typeof(HttpPostAttribute)) | |
let actionNameAttr = method.CustomAttributes.SingleOrDefault(x => x.AttributeType == typeof(ActionNameAttribute)) | |
let actionName = actionNameAttr != null ? actionNameAttr.ConstructorArguments.First().Value : method.Name | |
let controllerName = method.DeclaringType.Name.Substring(0, method.DeclaringType.Name.Length - "Controller".Length) | |
let methodNameBase = string.Format("{0}{1}", controllerName, method.Name) | |
select new | |
{ | |
MethodNameBase = methodNameBase, | |
ActionLinkMethodName = string.Format("{0}ActionLink", methodNameBase), | |
ControllerName = controllerName, | |
ActionName = actionName | |
}; | |
#> | |
/* | |
*************************************************************************************************************** | |
* WARNING: Do not edit this class. It is automatically generated using T4 and any changes will be overwritten* | |
*************************************************************************************************************** | |
*/ | |
using System.Web.Mvc; | |
using System.Web.Mvc.Html; | |
using Hermes.WebApplication.Controllers; | |
namespace Hermes.WebApplication.Extensions | |
{ | |
public static class ActionLinkExtensions | |
{ | |
<#foreach(var actionDefinition in actionDefinitions){#> | |
// ActionLink to: <#=actionDefinition.ControllerName#>Controller.<#=actionDefinition.ActionName#> | |
public static MvcHtmlString <#=actionDefinition.ActionLinkMethodName#>(this HtmlHelper htmlHelper, string linkText) | |
{ | |
return htmlHelper.<#=actionDefinition.ActionLinkMethodName#>(linkText, null, null); | |
} | |
public static MvcHtmlString <#=actionDefinition.ActionLinkMethodName#>(this HtmlHelper htmlHelper, string linkText, object routeValues) | |
{ | |
return htmlHelper.<#=actionDefinition.ActionLinkMethodName#>(linkText, routeValues, null); | |
} | |
public static MvcHtmlString <#=actionDefinition.ActionLinkMethodName#>(this HtmlHelper htmlHelper, string linkText, object routeValues, object htmlAttributes) | |
{ | |
return htmlHelper.ActionLink(linkText, "<#=actionDefinition.ActionName#>", "<#=actionDefinition.ControllerName#>", routeValues, htmlAttributes); | |
} | |
<#}#> | |
} | |
public static class ActionUrlExtensions | |
{ | |
<#foreach(var actionDefinition in actionDefinitions){#> | |
// Url for: <#=actionDefinition.ControllerName#>Controller.<#=actionDefinition.ActionName#> | |
public static string <#=actionDefinition.MethodNameBase#>(this UrlHelper urlHelper) | |
{ | |
return urlHelper.<#=actionDefinition.MethodNameBase#>(null); | |
} | |
public static string <#=actionDefinition.MethodNameBase#>(this UrlHelper urlHelper, object routeValues) | |
{ | |
return urlHelper.Action("<#=actionDefinition.ActionName#>", "<#=actionDefinition.ControllerName#>", routeValues); | |
} | |
<#}#> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment