简体   繁体   English

在ASP.NET中的几个页面之间共享功能的方式

[英]Ways to share common functions betweed few pages in ASP.NET

This is possibly very lame question and I lack knowledge about ASP.Net. 这可能是一个非常la脚的问题,我缺乏有关ASP.Net的知识。 In this case a link to an article explaining would be very welcome. 在这种情况下,非常欢迎您提供指向文章说明的链接。

I'm working on web-site on ASP.NET with C# as codebehind. 我正在ASP.NET上的网站上使用C#作为代码隐藏。 My current project involves developing few pages with very similar functionality and a many functions are the same. 我当前的项目涉及开发功能非常相似的几个页面,并且许多功能是相同的。 For example 例如

private void PermissionCheck()
{
    if (null == Session["UserID"] ||
        null == Session["ProjectID"] ||
        null == Session["AssetID"] ||
        null == Session["ProjectName"])
    {
        Response.Redirect("~/Login.aspx");
    }
 }

Would be the same on 2 pages. 在2页上将相同。 Some other things are the same as well. 其他一些东西也一样。 I would like to put this into common base class. 我想将其放入普通的基类。 But there are other functions that don't really belong to pages at all: 但是还有其他一些功能根本不属于页面:

private string GetAttachmentTempPath()
{
    return Request.PhysicalApplicationPath + WebConfigurationManager.AppSettings.Get("AttachmentsTempFolder");
}

I would like to move this into Attachment class, but to get the physical path of the application, I need to pass in Request object into that method, which is not really nice, as it couples Attachment class with Page.Request object. 我想将其移到Attachment类中,但是要获取应用程序的物理路径,我需要将Request对象传递到该方法中,这并不是很好,因为它将Attachment类与Page.Request对象耦合在一起。

Is there any way to move these functions somewhere else without needing to pass Page.Request objects around?? 有什么方法可以将这些功能移动到其他地方,而无需传递Page.Request对象吗?

ps The appliction is huge, and there is no scope to change the entire architecture. ps该应用程序非常庞大,没有改变整个体系结构的空间。

For your permission thing you could make a base page class: 为了获得许可,您可以创建一个基类:

class BasePage : Page
{ 
   ...
   protected override OnInit() {
     // do check here
   }
}

Now you can implement your page like this class MyOtherPage : BasePage { ... } The OnInit gets executed everytime your MyOtherPage gets loaded. 现在,您可以像此类class MyOtherPage : BasePage { ... }一样实现您的页面class MyOtherPage : BasePage { ... }每当您加载MyOtherPage时,就会执行OnInit。

You can see a complete page lifecycle here: Link 您可以在此处查看完整的页面生命周期: 链接

For your other problem: Consider to implement a global available static tool class 对于您的其他问题:考虑实现全局可用的静态工具类

Update 更新

A good approach for making things like web.config easier to access is a Singleton. 一个使web.config之类的东西更易于访问的好方法是Singleton。 In asp.net a singleton is only created once and lives until the asp worker process gets stopped . 在asp.net中,一个单例仅创建一次,并且一直存在直到asp worker进程停止为止。 So this values are shared across the whole asp.net application. 因此,此值在整个asp.net应用程序中共享。 This is also good for storing data in a global context that you dont want to get out of your database or file anytime a user makes a page request (for example a version number or things like that) 这也适用于在用户发出页面请求(例如版本号之类)时不希望从数据库或文件中移出的全局上下文中存储数据。

Update 2 更新2

To access the request without passing it to every function, use this: 要访问请求而不将其传递给每个函数,请使用以下命令:

HttpContext.Current.Request

Base page is a good option for reusable code in Page level. 对于页面级别的可重用代码,基本页面是一个不错的选择。 Other than that for things like configuration values it's good to come up with utility classes specifically for those methods base don there type of operations. 除此之外,对于诸如配置值之类的东西,最好专门针对那些没有此类操作的方法提供实用程序类。 If you need to avoid passing in Page object into these types of utility methods, 如果您需要避免将Page对象传入这些类型的实用程序方法,

HttpContext

class would be handy because you can access many of the ASP.Net object through static members through this class. 该类非常方便,因为您可以通过此类通过静态成员访问许多ASP.Net对象。 HttpConext @ MSDN HttpConext @ MSDN

If you have similar functions behind the page, you can use ASP.NET WEb User Control . 如果页面后面具有类似功能,则可以使用ASP.NET WEb用户控件

You can create the functions in the usercontrol and use the control in the pages where necessary. 您可以在用户控件中创建功能,并在必要时使用页面中的控件。

Have look at this article about Web User Control in Asp.Net 在Asp.Net中查看有关Web用户控件的这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM