Skip to content

Instantly share code, notes, and snippets.

@jgauffin
Created October 8, 2015 19:36
Show Gist options
  • Select an option

  • Save jgauffin/55b1ce86c7e5fc5cced8 to your computer and use it in GitHub Desktop.

Select an option

Save jgauffin/55b1ce86c7e5fc5cced8 to your computer and use it in GitHub Desktop.
How to modify metadata
public class BaseController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var keywords = filterContext.ActionDescriptor.GetCustomAttributes(typeof(MetaKeywordsAttribute), false);
if (keywords.Length == 1)
ViewData["MetaKeywords"] = ReplaceRouteData(((MetaDescriptionAttribute)keywords[0]).Value);
base.OnActionExecuting(filterContext);
}
private string ReplaceRouteData(string value)
{
foreach (var kvp in ControllerContext.RouteData.Values)
{
value = value.Replace("{" + kvp.Key + "}", kvp.Value.ToString());
}
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment