How does ASP. NET obtain each part of the request URL?

Source: Internet
Author: User
Tags string indexof

When developing a web application, we often need to parse each segment of the request. url and make some judgments. For example, "http: // localhost: 1897/news/press/content. aspx/123? Id = 1 # TOC
", We want to get the name of the first category in the website (News) to determine different levels of subject (page title ).

I think many people use the string indexof method and substring method:

Request. url. pathandquery. substring (1, request. url. pathandquery. indexof ("/", 1)-1)

This is too buried. net, in fact, the request object has provided many convenient property fragments for obtaining the website.

The following table lists the relevance and usage of various browser request URLs:

Website: http: // localhost: 1897/news/press/content. aspx/123? Id = 1 # TOC
Request. applicationpath /
Request. physicalpath D: \ projects \ solution \ WEB \ news \ press \ content. aspx
System. Io. Path. getdirectoryname (request. physicalpath) D: \ projects \ solution \ WEB \ news \ Press
Request. physicalapplicationpath D: \ projects \ solution \ WEB \
System. Io. Path. getfilename (request. physicalpath) Content. aspx
Request. currentexecutionfilepath /News/press/content. aspx
Request. filepath /News/press/content. aspx
Request. Path /News/press/content. aspx/123
Request. rawurl /News/press/content. aspixels/123? Id = 1
Request. url. absolutepath /News/press/content. aspx/123
Request. url. absoluteuri Http: // localhost: 1897/news/press/content. aspx/123? Id = 1
Request. url. Scheme HTTP
Request. url. Host Localhost
Request. url. Port 1897
Request. url. Authority Localhost: 1897
Request. url. localpath /News/press/content. aspx/123
Request. pathinfo /123
Request. url. pathandquery /News/press/content. aspixels/123? Id = 1
Request. url. Query ? Id = 1
Request. url. Fragment  
Request. url. segments /

News/

Press/

Content. aspx/

123

So after you read this table, you still want to use request. URL. pathandquery. substring (1, request. URL. pathandquery. indexof ("/", 1)-1?

Using the Response Request. url. segments [1]. Replace ("/", "") Is it short and straight? Pai_^

The program that generates the above table is as follows:

Protected
Void
Page_load (Object
Sender, eventargs E)
{

Stringbuilder sb = new
Stringbuilder ();

SB. append ("<Table cellpadding = 3 cellspacing = 0 border = 1> ");

SB. append ("<tr> <TD colspan = 2> ");
SB. append ("Website:" http: // localhost: 1897/news/press/content. aspx/123? Id = 1 # TOC "> http: // localhost: 1897/news/press/content. aspx/123? Id = 1 # TOC

");
SB. append ("</TD> </tr> ");

// Request. applicationpath

SB. append ("<tr> <TD> ");
SB. append ("request. applicationpath ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. applicationpath + "</B> ");
SB. append ("</TD> </tr> ");

// Request. physicalpath

SB. append ("<tr> <TD> ");
SB. append ("request. physicalpath ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. physicalpath + "</B> ");
SB. append ("</TD> </tr> ");

// System. Io. Path. getdirectoryname (request. physicalpath)

SB. append ("<tr> <TD> ");
SB. append ("system. Io. Path. getdirectoryname (request. physicalpath )");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + system. Io. Path. getdirectoryname (request. physicalpath) + "</B> ");
SB. append ("</TD> </tr> ");

// Request. physicalapplicationpath

SB. append ("<tr> <TD> ");
SB. append ("request. physicalapplicationpath ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. physicalapplicationpath + "</B> ");
SB. append ("</TD> </tr> ");

// System. Io. Path. getfilename (request. physicalpath)

SB. append ("<tr> <TD> ");
SB. append ("system. Io. Path. getfilename (request. physicalpath )");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + system. Io. Path. getfilename (request. physicalpath) + "</B> ");
SB. append ("</TD> </tr> ");

// Request. currentexecutionfilepath

SB. append ("<tr> <TD> ");
SB. append ("request. currentexecutionfilepath ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. currentexecutionfilepath + "</B> ");
SB. append ("</TD> </tr> ");

// Request. filepath

SB. append ("<tr> <TD> ");
SB. append ("request. filepath ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. filepath + "</B> ");
SB. append ("</TD> </tr> ");

// Request. Path

SB. append ("<tr> <TD> ");
SB. append ("request. Path ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. Path + "</B> ");
SB. append ("</TD> </tr> ");

// Request. rawurl

SB. append ("<tr> <TD> ");
SB. append ("request. rawurl ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. rawurl + "</B> ");
SB. append ("</TD> </tr> ");

// Request. url. absolutepath

SB. append ("<tr> <TD> ");
SB. append ("request. url. absolutepath ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. url. absolutepath + "</B> ");
SB. append ("</TD> </tr> ");

// Request. url. absoluteuri

SB. append ("<tr> <TD> ");
SB. append ("request. url. absoluteuri ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. url. absoluteuri + "</B> ");
SB. append ("</TD> </tr> ");

// Request. url. Scheme

SB. append ("<tr> <TD> ");
SB. append ("request. url. Scheme ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. url. scheme + "</B> ");
SB. append ("</TD> </tr> ");

// Request. url. Host

SB. append ("<tr> <TD> ");
SB. append ("request. url. Host ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. url. Host + "</B> ");
SB. append ("</TD> </tr> ");

// Request. url. Port

SB. append ("<tr> <TD> ");
SB. append ("request. url. Port ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. url. Port + "</B> ");
SB. append ("</TD> </tr> ");

// Request. url. Authority

SB. append ("<tr> <TD> ");
SB. append ("request. url. Authority ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. url. Authority + "</B> ");
SB. append ("</TD> </tr> ");

// Local request. url. localpath

SB. append ("<tr> <TD> ");
SB. append ("request. url. localpath ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. url. localpath + "</B> ");
SB. append ("</TD> </tr> ");

// Request. pathinfo

SB. append ("<tr> <TD> ");
SB. append ("request. pathinfo ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. pathinfo + "</B> ");
SB. append ("</TD> </tr> ");

// Request. url. pathandquery

SB. append ("<tr> <TD> ");
SB. append ("request. url. pathandquery ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. url. pathandquery + "</B> ");
SB. append ("</TD> </tr> ");

// Request. url. Query

SB. append ("<tr> <TD> ");
SB. append ("request. url. Query ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. url. query + "</B> ");
SB. append ("</TD> </tr> ");

// Request. url. Fragment

// In principle, you cannot obtain any information from request. url. fragment, because the browser usually does not send # TOC.

SB. append ("<tr> <TD> ");
SB. append ("request. url. Fragment ");
SB. append ("</TD> <TD> ");
SB. append ("<B>" + request. url. Fragment + "</B> ");
SB. append ("</TD> </tr> ");

// Request. url. segments

SB. append ("<tr> ");
SB. append ("<TD> ");
SB. append ("request. url. segments ");
SB. append ("</TD> ");
SB. append ("<TD> ");
String
[] Segments = request. url. segments;
Foreach
(String
S in
Segments)
{
SB. append ("<B>" + S + "</B> ");
SB. append ("<br/> ");
}
SB. append ("</TD> ");
SB. append ("</tr> ");

SB. append ("</table> ");

Ltltable. Text = sb. tostring ();
}

Attention

  • When deploy a program to IIS 6, your website is set to http: // localhost: 1897/news/press/content. aspx/123

    ?
    IIS does not know why it will ask question (? So that ASP. NET uses request. url. query as a null string! If your program judges this part, you must pay special attention to it, because it should be caught as appropriate! I don't know if it is a bug in IIS 6?
  • [Reference to the Will Web

    ]
Related Article

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.