如何在多个 ASP.NET 页面上重写相同的函数(渲染函数)
采取以下场景。我有多个 ASPX 页面。 Login、Logout、Main、Messages等等...当然它们都继承自System.Web.UI.Page。对于所有页面,我想重写 Page 类中的 Render 方法。我可以轻松地将相同的代码复制并粘贴到每个页面中,如下所示:
protected override void Render(HtmlTextWriter writer)
{
//Code Logic Here
}
但是,如果我有很多页面,比如说 20 个页面,那么维护每个页面中的代码可能会非常耗时且容易出错。
这让我思考了一下,我想好吧,让我们尝试一下......覆盖每个页面中的函数,但调用静态函数。这样,更改静态函数将导致每个页面发生更改。效果很好...但它并不是很好也很干净,必须在每个页面上都进行这样的覆盖。有人对此有什么想法或想法吗?也许我忽略了一些简单的事情?谢谢
编辑:有些页面使用 System.Web.UI.Page 类,有些页面继承另一个名为 ModifiedPage 的类,该类继承并覆盖 System.Web.UI.Page 类的其他功能。所以它不像从一个类继承所有页面那么简单。
EDIT2:所有页面都需要这种行为,有些页面已经从另一个类派生,并且我无法更改该其他类的实现或继承层次结构。
Take the following scenario. I have multiple ASPX pages. Login, Logout, Main, Messages, etc... They all inherit from System.Web.UI.Page of course. For all the pages, I want to override the Render method from the Page class. I could easily copy and paste the same code into each page like so:
protected override void Render(HtmlTextWriter writer)
{
//Code Logic Here
}
But if I had many pages, lets say 20, maintaining the code in each page could get very time consuming and error prone.
That made me think a bit and I thought okay lets try this...override the function in each page but call a static function. That way changing the static function would result in a change for every page. Which works fine... But its not really nice and clean, having to override like that on every single page. Anybody have any ideas or thoughts on this one? Perhaps something simple I am overlooking? Thanks
EDIT: Some pages use the System.Web.UI.Page class and some pages inherit from another class called ModifiedPage which inherits and overridies other functions of the System.Web.UI.Page class. So its not as simple as inheriting all the pages from one class.
EDIT2: All pages need this behavior, some already derive from another class, and I am unable to change the implementation or inheritance hierarchy of that other class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要从
System.Web.UI.Page
继承,而是让它们全部从MyProject.MyBasePage
继承,而MyProject.MyBasePage
从 Page 继承:并且...
编辑
现在添加到问题中的澄清指出了真正的难题 - 都需要这种公共渲染逻辑的页面具有不同的继承路径。这在 C# 中更加棘手,并且您将无法避免至少一点冗余的管道代码。有很多不同的方法来处理这个问题 - 这是我过去采用的一种方法:
1)为此通用功能创建一个接口。例如,
IOverrideRender
:2) 需要此功能的每个页面都会获取接口并像这样连接它:
3) 在 HttpModule,检查处理程序是否为
IOverrideRender
,如果是,则传入您的自定义渲染方法:Instead of inheriting from
System.Web.UI.Page
, have them all inherit fromMyProject.MyBasePage
which inherits from Page:and...
Edit
Clarification added to the question now points out the real puzzle - the pages which all need this common Render logic have different inheritance paths. That is more tricky in c#, and you won't be able to avoid at least a little bit of redundant plumbing code. There's plenty of different ways to handle this - here's one approach I have taken in the past:
1) Create an interface for this common functionality. For example,
IOverrideRender
:2) Each page which needs this functionality gets the interface and wires it like so:
3) In an HttpModule, check to see if the handler is
IOverrideRender
and if so, pass in your custom render method:您应该创建一个继承自 System.Web.UI.Page 的 BasePage。在 BasePage 中,您可以重写 Render 方法,然后让所有页面继承自 BasePage。
You should create a BasePage which inherits from System.Web.UI.Page. Within the BasePage you could override the Render method and then have all your pages inherit from BasePage.
在层次结构中添加一个级别并创建一个
BasePage
并在那里进行覆盖,然后从BasePage
继承所有其他页面Add a level in your hierarchy and make a
BasePage
and do your override there, then inherit all other page fromBasePage