Asp. NET Global Application Classes

Source: Internet
Author: User

Asp. NET Global Application Classes from the old man's blog http://shijiaqi1066.iteye.com/blog/1728603 This is saved to your blog inside to learn to see later

Global file

New Global file

Visual Studio Add New Item--"Web"--"Global Application Class"

produces the global class.

A webapplication can have only one global application class. If the new discovery does not have a global application class, it means that the class already exists, and if you need to create a new, delete the old ones before creating them.

Global class:

C # code
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Web;
  5. Using System.Web.Security;
  6. Using System.Web.SessionState;
  7. Namespace WebApplication1
  8. {
  9. public class Global:System.Web.HttpApplication
  10. {
  11. protected void Application_Start (object sender, EventArgs e)
  12. {
  13. //code that runs when the application starts.
  14. //The method is executed the first time the program obtains the request.
  15. }
  16. protected void session_start (object sender, EventArgs e)
  17. {
  18. //code that runs when a new session is started.
  19. //The method is called, indicating that there is currently a new session generated.
  20. }
  21. protected void Application_BeginRequest (object sender, EventArgs e)
  22. {
  23. //code that is allowed when a new request is started.
  24. //Each request triggers the method.
  25. }
  26. protected void Application_AuthenticateRequest (object sender, EventArgs e)
  27. {
  28. //code that runs when the application starts.
  29. //The method is executed the first time the program obtains the request.
  30. }
  31. protected void Application_Error (object sender, EventArgs e)
  32. {
  33. //code that runs when an unhandled error occurs
  34. //Get exception information and process: HttpContext.Current.Server.GetLastError ();
  35. }
  36. protected void Session_End (object sender, EventArgs e)
  37. {
  38. //code that runs at the end of the session.
  39. //Note: The Session_End event is raised only if the sessionstate mode in the Web. config file is set to InProc.
  40. //If the session mode is set to StateServer or SQL Server, the event is not raised.
  41. }
  42. protected void Application_End (object sender, EventArgs e)
  43. {
  44. //code to run when the application shuts down
  45. }
  46. }
  47. }

Application:

Session_Start () Method: Count the number of people online.

Application_BeginRequest () Method: Block IP, prevent hotlinking. Yes, so the picture is watermark.

Application_AuthenticateRequest () Method: Validation method.

Application_Error () Method:

Add:

1. Get the current request URL:HTTPCONTEXT.CURRENT.REQUEST.URL

2. Manual Logoff Session:HttpContext.Current.Session.Abandon ()

3. Add text information to the file and create it first if the file does not exist: File.appendalltext ("C:1.txt", DateTime.Now.ToString ())

4. Ip:HttpContext.Current.Request.UserHostAddress of requests for access to the website;

5. Print output to the page: HttpContext.Current.Request.Write ("has been blocked! ");

6. Print output end to call: HttpContext.Current.Request.End ();

7. Get Request Type: HttpContext.Current.Request.ContentType

8. Get URL information for client last request: HttpContext.Current.Request.UrlReferrer; (Description: This property is an instance of the URI class.) )

9. Gets the domain name of the URI object: URI. Host;

(Gets the domain name of the URL that the client last requested: HttpContext.Current.Request.UrlReferrer.Host;)

10. Get exception information: HttpContext.Current.Server.GetLastError ();

ASP. NET global Application class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.