Shared Global.asax & Error.aspx files
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.
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.
精彩评论