简体   繁体   中英

How can i override Url.Action

Now i use to override extensions like :

public abstract class MyWebViewPage<T> : WebViewPage<T>
{
    public new MyHtmlHelper<T> Html { get; set; }
    public override void InitHelpers()
    {
        Ajax = new AjaxHelper<T>(ViewContext, this);
        Url = new UrlHelper(ViewContext.RequestContext);
        Html = new MyHtmlHelper<T>(ViewContext, this);
    }
}

public class MyHtmlHelper<T> : HtmlHelper<T>
{
    public MyHtmlHelper(ViewContext viewContext, IViewDataContainer viewDataContainer) :
        base(viewContext, viewDataContainer)
    {
    }

    public MvcHtmlString ActionLink(string linkText, string actionName)
    {
        return ActionLink(linkText, actionName, null, new RouteValueDictionary(), new RouteValueDictionary());
    } 
}

How to add Url.Action helper here with all overloaded version?

UPD: I should override all standard method because many peoples work on this and i they should use standard helpers but with my functionality

You don't need to override neither the Url.Action helper nor the HtmlHelper actions. You can create Extension Methods instead. Here's an example:

public static class MyHelpers
{
    public static string MyAction(this UrlHelper url, string actionName)
    {
        // return whatever you want (here's an example)...
        return url.Action(actionName, new RouteValueDictionary());
    }
}

Then, you can use the method in your views like this:

@Url.MyAction("MyActionName")

UPDATE:

I wouldn't recommend overriding the Url.Action method. Creating extension methods is much easier and cleaner. But, here's how you can do it:

public class MyUrlHelper : UrlHelper 
{
    public override string Action(string actionName)
    {
        return base.Action(actionName, new RouteValueDictionary());  
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM