共享 Global.asax 和错误.aspx 文件

发布于 2024-08-24 01:04:22 字数 502 浏览 8 评论 0 原文

我的解决方案中有多个 ASP.NET 网站以及一个常见的 C# 代码项目。内容如下:

  • Website1
  • Website2
  • ...
  • Website(n)
  • Common

首先我想使用 Global.asax 文件来记录所有未处理的异常。我可以为每个网站拥有一个 Global.aspx 文件,但所有代码都是相同的,并且必须保持同一文件的多个副本最新似乎毫无意义。有没有办法将 Global.aspx 文件放在公共库中,然后从每个网站链接到它。我尝试过添加退出项目>添加为链接,但页面不会运行它。

其次,所有网站都需要一个错误页面,我可以在其中写入信息(不仅仅是一个简单的 html 页面),因此我有一个 Error.aspx 页面,我想将其与错误详细信息一起重定向到。同样,我可以每个网站都有一个,但它会是同一个页面。有没有办法可以将其存储在公共项目中并链接到它?到目前为止,我能想到的唯一解决方案是建立一个包含错误页面的错误网站。

I have several ASP.NET websites in a solution along with a common C# code project. Something as follows:

  • Website1
  • Website2
  • ...
  • Website(n)
  • Common

First off I want to use a Global.asax file to log all unhandled exceptions. I could have one Global.aspx file per website but all the code will be the same and it seems pointless having to keep multiple copies of the same file upto date. Is there a way to have the Global.aspx file in the Common library and then link to it from each website. I have tried doing Add Exiting Item > Add as Link but it doesn't get run by the page.

Secondly all the websites need an Error page which I can write information to (not just a simple html page) so I have an Error.aspx page which I want to redirect to along with the error details. Again I could have one per website, but its going to be the same page. Is there a way I can store this in the Common project and link to it? The only solution so far for this I can think of is to have an Error website which has the Error page in it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人心善变 2024-08-31 01:04:22

您可以从基类继承 Global.asax 文件。所以你的 global.asax 可能看起来像这样:

<%@ Application Codebehind="Global.asax.cs" Inherits="MyApplication" Language="C#" %>

然后你的 Global.asax.cs 看起来像这样:

public class MyApplication: BaseApplication 
{
    protected void Application_Start()
    {
        // application specific code... if any
    }
}

在你的库中定义:

public class BaseApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        // do your shared code here
        base.Application_Start();
    }
}

就你的“全局”错误页面而言,我要做的是创建一个处理程序(. ashx)每个网站上的页面,然后制作一个库方法来显示您的错误信息。像这样的东西 (Error.ashx)

<%@ WebHandler Language="C#" Class="errorhandler" %>

public class errorhandler: IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        MyLibrary.ErrorHandler(context);
    }
}

注意:为了清楚起见,示例中未包含命名空间。

You can inherit your Global.asax file from a base class. So your global.asax might look like this:

<%@ Application Codebehind="Global.asax.cs" Inherits="MyApplication" Language="C#" %>

Then your Global.asax.cs would look something like this:

public class MyApplication: BaseApplication 
{
    protected void Application_Start()
    {
        // application specific code... if any
    }
}

In your library define:

public class BaseApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        // do your shared code here
        base.Application_Start();
    }
}

As far as your "global" error page is concerned, what I would do is create a handler (.ashx) page on each website and then make a library method to display your error information. Something like this (Error.ashx)

<%@ WebHandler Language="C#" Class="errorhandler" %>

public class errorhandler: IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        MyLibrary.ErrorHandler(context);
    }
}

Note: Namespaces not included in example for clarity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文