Asp.net to obtain the current page file name, parameters, domain names, and other methods. Unified session verification and permission verification methods

Source: Internet
Author: User

Asp.net background to get the file name of the current page

System. Io. Path. getfilename (request. Path). tostring ();

Obtain the file name, parameters, and domain name of the current page.

Suppose the full address of the current page is: http://www.test.com/aaa/bbb.aspx? Id = 5 & name = kelli

"Http: //" is the protocol name.

"Www.test.com" is a domain name

"AAA" is the virtual directory name

"BBB. aspx" is the page name (file name)

"Id = 5 & name = Kelli" is a parameter

[1] obtain the complete URL (protocol name + domain name + virtual directory name + file name + parameter)

String url = request. url. tostring ();

Url = http://www.test.com/aaa/bbb.aspx? Id = 5 & name = kelli

[2] obtain the virtual directory name + Page name + parameter:

String url = request. rawurl;

(Or string url = request. url. pathandquery ;)

Url =/AAA/BBB. aspx? Id = 5 & name = kelli

[3] obtain the virtual directory name + Page name:

String url = httpcontext. Current. Request. url. absolutepath;

(Or string url = httpcontext. Current. Request. Path ;)

Url =/AAA/BBB. aspx

[4] retrieve domain names:

String url = httpcontext. Current. Request. url. Host;

Url = www.test.com

[5] GET parameters:

String url = httpcontext. Current. Request. url. query;

Url =? Id = 5 & name = kelli

Request. querystring ["ID"] and request. querystring ["name"] Access Parameters

Request. urlreferrer can obtain the URL Information of the client's last request, so that we can return the "Previous Page" through this attribute ".

Similarly, request. urlreferrer. query can obtain the relevant parameters of the client's last request URL.

Unified Verification:

Recently I found a good method for unified verification. I wrote it here that I passed the test, but it is not very clear whether it is inappropriate, we also hope to have a high finger.

Create a class file under the appcode folder, inherit the page class, add the page. Load event to the file, and then inherit the class from the file. CS to be verified. Example

Create a class file for basepage_mana.cs:

View plaincopy to clipboardprint?
  1. Using system. collections;
  2. Using system. Web;
  3. Using system. Web. Security;
  4. Using system. Web. UI;
  5. Using system. Web. UI. webcontrols;
  6. Using system. Web. UI. webcontrols. webparts;
  7. Using system. Web. UI. htmlcontrols;
  8. Using webutil;
  9. Using sysmodel;
  10. Using sysdal;
  11. /// <Summary>
  12. /// Let all the background management pages inherit this class, and perform session verification and permission verification here.
  13. /// </Summary>
  14. Public class basepage_mana: system. Web. UI. Page
  15. {
  16. Public basepage_mana ()
  17. {
  18. This. Load + = new eventhandler (thisload );
  19. //
  20. // Todo: the verification process is to be completed (todo: a prompt for unfinished work)
  21. //
  22. }
  23. Void thisload (Object sender, eventargs E)
  24. {
  25. # The warning verification program needs to be improved (warning is the warning setting)
  26. If (system. String. isnullorempty (session ["bn_jobnum"] as string ))
  27. {
  28. Response. Write ("<MCE: script language = 'javascript '> <! --
  29. Alert ('Please log in again! '); Parent. Location. href =' ../login_test.aspx ';
  30. // --> </MCE: SCRIPT> ");
  31. Response. End ();
  32. }
  33. Purviewdal pdal = new purviewdal ();
  34. String pagename = system. io. path. getfilename (request. path ). tostring (); // I have made a permission judgment class here. I regard the file name as the permission name, so that I can make a unified judgment.
  35. If (pdal. powercontrol (session ["bn_jobnum"]. tostring (), pagename) = false)
  36. {
  37. Response. Write ("<MCE: script language = JavaScript> <! --
  38. Alert ('You have no permission! '); History. Go (-1 );
  39. // --> </MCE: SCRIPT> ");
  40. Response. End ();
  41. }
  42. }
  43. }

Then inherit the class from the file test. aspx.

View plaincopy to clipboardprint?
  1. Using system;
  2. Using system. Data;
  3. Using system. configuration;
  4. Using system. collections;
  5. Using system. Web;
  6. Using system. Web. Security;
  7. Using system. Web. UI;
  8. Using system. Web. UI. webcontrols;
  9. Using system. Web. UI. webcontrols. webparts;
  10. Using system. Web. UI. htmlcontrols;
  11. Using system. Collections. Generic;
  12. Using onlineuser;
  13. Using sysmodel;
  14. Using sysdal;
  15. Using webutil;
  16. Public partial class test: basepage_do // inherit the class created
  17. {
  18. Protected void page_load (Object sender, eventargs E)
  19. {}
  20. }

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.