[英]How can i override Url.Action
现在我用来覆盖扩展名,如:
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());
}
}
如何在这里添加所有重载版本的Url.Action
助手?
UPD:我应该覆盖所有标准方法,因为许多人都在这方面工作,我应该使用标准助手,但我的功能
您既不需要覆盖Url.Action
帮助程序,也不需要覆盖HtmlHelper
操作。 您可以改为创建扩展方法 。 这是一个例子:
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());
}
}
然后,您可以在视图中使用此方法,如下所示:
@Url.MyAction("MyActionName")
更新:
我不建议覆盖Url.Action
方法。 创建扩展方法更容易,更清洁。 但是,这是你如何做到的:
public class MyUrlHelper : UrlHelper
{
public override string Action(string actionName)
{
return base.Action(actionName, new RouteValueDictionary());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.