How can I dynamically redefine the URL generated by Html.ActionLink?

I implemented a small utility on an existing ASP.NET MVC website that allows the user to redefine the current HTML heading, meta description and friendly URL for any page (including dynamic pages with query strings, etc.). This works just fine - and all is well when you visit a friendly URL directly.

What I would like to do is to override the default Html.ActionLink behavior so that I can check if a friendly URL was created for the requested action, and if so, return the friendly URL instead of the automatically generated URL.

I looked around and could not find anything specific. I know that I can just implement the new HtmlHelper and do it myself, but if possible, I would like to override the existing behavior, and not change all the current views.

Can anyone help?

+4
source share
1 answer

I don’t think you can override the action link, but you can create an extension method and use it instead 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 opinion you can replace @ Html.ActionLink with @ Html.CustomActionLink. You must add a namespace at the beginning of each view, do not put it in the namespace, or add it to the configuration file

+2

Source: https://habr.com/ru/post/1538784/


All Articles