Reputation: 881
I've implemented a small utility within an existing ASP.NET MVC website which allows the user to override the current HTML title, meta description, and friendly-url for any page (including dynamic pages with query-strings etc). This works perfectly well - and everything is fine when visiting the friendly-url directly.
What'd I'd like to be able to do is to override the default behaviour of Html.ActionLink so that I can check whether a friendly URL has been created for the requested action and, if so, return the friendly url in place of the automatically generated URL.
I've had a look around and haven't been able to find anything concrete. I know I can just implement a new HtmlHelper and do this myself, but if possible I'd like to override the existing behaviour rather than have to modify all of the current views.
Is anybody able to help?
Upvotes: 4
Views: 1155
Reputation: 394
I don't think you can override the action link its self but you could create an extension method and use it in place of the actionlink helper
using System.Web.Mvc;
using System.Web.Mvc.Html;
public static class HtmlHelperExtensions
{
public static MvcHtmlString CustomActionLink(this HtmlHelper Helper, string LinkText, string ActionName)
{
string link = getCustomName();
if(!String.IsNullOrWhiteSpace(link))
{
return link
}
return helper.ActionLink(LinkText, ActionName);
}
}
then in your view you can replace @Html.ActionLink, with @Html.CustomActionLink. You have to add the namespace to the top of each view, don't put it in a namespace, or add it to the config file
Upvotes: 2