Ale*_*ksP 2 c# asp.net-mvc extension-methods asp.net-mvc-3
现在我用来覆盖扩展名,如:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
如何在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());
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在视图中使用此方法,如下所示:
@Url.MyAction("MyActionName")
Run Code Online (Sandbox Code Playgroud)
更新:
我不建议覆盖该Url.Action方法.创建扩展方法更容易,更清洁.但是,这是你如何做到的:
public class MyUrlHelper : UrlHelper
{
public override string Action(string actionName)
{
return base.Action(actionName, new RouteValueDictionary());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2522 次 |
| 最近记录: |