您将需要介绍自己的WebViewPage
,它提供自己的实现,UrlHelper
该实现将覆盖该Content()
方法。
首先,创建类型:
public class MyUrlHelper : UrlHelper
{
public MyUrlHelper() {}
public MyUrlHelper(RequestContext requestContext) : base(requestContext) {}
public MyUrlHelper(RequestContext requestContext, RouteCollection routeCollection) : base(requestContext, routeCollection) { }
public override string Content(string contentPath)
{
// do your own custom implemetation here,
// you access original Content() method using base.Content()
}
}
public abstract class MyWebPage : WebViewPage
{
protected override void InitializePage()
{
this._urlHelper = new MyUrlHelper(this.Request.RequestContext, RouteTable.Routes);
}
private MyUrlHelper _urlHelper;
public new MyUrlHelper Url { get { return _urlHelper; } }
}
// provide generic version for strongly typed views
public abstract class MyWebPage<T> : WebViewPage<T>
{
protected override void InitializePage()
{
this._urlHelper = new MyUrlHelper(this.Request.RequestContext, RouteTable.Routes);
}
private MyUrlHelper _urlHelper;
public new MyUrlHelper Url { get { return _urlHelper; } }
}
MyWebPage
然后在以下位置注册您的自定义~/Views/Web.Config
:
<system.web.webPages.razor>
....
<pages pageBaseType="Your.NameSpace.MyWebPage">
....
</pages>
</system.web.webPages.razor>