[英]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? 如何在这里添加所有重载版本的Url.Action
助手?
UPD: I should override all standard method because many peoples work on this and i they should use standard helpers but with my functionality UPD:我应该覆盖所有标准方法,因为许多人都在这方面工作,我应该使用标准助手,但我的功能
You don't need to override neither the Url.Action
helper nor the HtmlHelper
actions. 您既不需要覆盖Url.Action
帮助程序,也不需要覆盖HtmlHelper
操作。 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. 我不建议覆盖Url.Action
方法。 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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.