Skip to content

Instantly share code, notes, and snippets.

@jasonmitchell
Last active December 14, 2015 10:18
Show Gist options
  • Save jasonmitchell/5070743 to your computer and use it in GitHub Desktop.
Save jasonmitchell/5070743 to your computer and use it in GitHub Desktop.
<#@ 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