Implementation Scheme of asp.net 2.0 for second-level website domain names

Source: Internet
Author: User
Tags website domain names
I understand two solutions. I hope you can discuss them more!

Basic Ideas:
1. Domain Name supports wildcard resolution, that is, resolve a record *. Domain Name. com to the server IP address, bind it to the server IIS, and the Host header is blank when binding;
2. to implement a complete second-level domain, create two sites, one for the master site, the other for the user, and both site directories refer to the same website directory.
3. host headers of second-level domain names in web programs or URLs, such as ABC in ABC. Domain Name. com;
4. Use the obtained second-level domain name and store it in the session for easy access
5. Use the obtained second-level domain name and URL to rewrite the address.

Implementation Method:
Domain name a record resolution Needless to say ^ _ ^, is to make a *. Domain Name. com a record resolved to your server IP

Method 1: second-level domain name URL redirection
A. Create a site and bind a domain name (win2003-IIS6) to IIS)
Open IIS, right-click the site, and click Properties. Click the Advanced button of the website item IP address, and click Edit or add to add a new binding. The host header value is blank.

The following code is used to obtain and determine the Host Header. The code is placed in index. aspx. CS, the first file of the default document.

 

Code:
/// <Summary>
/// Obtain the second-level domain host header value and perform redirection
/// </Summary>
Public void checkdomain ()
{
Hostname = httpcontext. Current. Request. url. Host. tostring (); // obtain the URL host address
Userhost = hostname. Split (New char [] {'.'}); // array, separated "."

// Determine whether the second-level domain name address complies with the ABC. Domain Name. com format, and the length of the array Userhost is not greater than 3. Otherwise, the second-level domain name will jump to other pages.
If (Userhost. length> 3)
{
Httpcontext. Current. response. Redirect ("http: // www. Domain Name. com/error. aspx"); // jump to the error page
Return;
}

Userdomainname = Userhost [0]. tostring (); // obtain the first group of values in the array and the Host header of the second-level domain name

// Make specific judgments and do not use the Host Header as the second-level domain name
If (userdomainname. tolower () = "www" | userdomainname. tolower () = "Domain Name" | userdomainname = NULL | userdomainname. tostring () = "")
{
// Your actions
}
Else {
HttpContext. Current. Response. Redirect ("/User/"); // jump to the User directory, that is, the directory to which the second-level domain name is going. Of course, you can also jump to *. aspx? UserID = xxx
Return;
}

}

Now, abc. Domain Name. com can be redirected to a specified page or link, but it is not a real second-level domain name, but a URL redirection.

Method 2: real second-level domain name
A. Create a site
At this point, we need to create two sites, one primary site and one second-level domain site. The file directories of the two sites are the same directory, and the directories include default. aspx and index. aspx. The method is as follows:
A ). the method for creating a primary site is described in method 1. However, the host header is not empty and must be set to www. domain name. COM and domain name. com, of course, you can also set other host headers that do not want to be used as second-level domains. The default website access document is default. aspx.
B). The method for creating a second-level domain site is the same as that for creating a site in method 1. The default access document is index. aspx.

B. Now we need to use the previously established second-level domain site. We will store user data in the user directory under the root directory.
The following is the default document (index. aspx. CS) Process Code, mainly used to store the second-level domain name Host header into the session for convenient calling, is also the homepage file of the second-level domain (User zone)

 

Code:
/// <Summary>
/// Obtain the Host Header Value of the second-level domain and store it in session ["userdomainname "]
/// </Summary>
Public void userdomainnamesession ()
{
HostName = HttpContext. Current. Request. Url. Host. ToString (); // obtain the URL Host address
UserHost = HostName. Split (new Char [] {'.'}); // array, separated "."

// Determine whether the second-level domain name address complies with the abc. Domain Name. com format, and the length of the array UserHost is not greater than 3. Otherwise, the second-level domain name will jump to other pages.
If (UserHost. Length> 3)
{
Httpcontext. Current. response. Redirect ("http: // www. Domain Name. com // error. aspx"); // jump to the error page
Return;
}

Userdomainname = Userhost [0]. tostring (); // obtain the first group of values in the array and the Host header of the second-level domain name

// Make specific judgments and do not use the Host Header as the second-level domain name
If (userdomainname. tolower () = "www" | userdomainname. tolower () = "Domain Name" | userdomainname = NULL | userdomainname. tostring () = "")
{
// Your actions
}
Else
{
Httpcontext. Current. session ["userdomainname"] = userdomainname; // Save the Host header of the second-level domain name to the session
}
}

// Your processing of Session ["UserDomainName"]. For example, if the value of this Session ["UserDomainName"] is "abc", you can index. aspx? UserName = abc. If you do not want to use Session, you can use the URL address to obtain the Host header of the second-level domain.

C. URL rewriting
I am using Microsoft's URLRewriter. For how to use it, see: http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.......g.mspx? Mfr = true
The rewrite method in web. config is as follows:
<! -- Rewrite the Host Header URL in the User area when abc. domain name. when accessing the site through com, the first file is Index by default. aspx, Index. override the aspx address to/User/Index. aspx -->

Code: [copy to clipboard]
<RewriterRule>
<LookFor> ~ /Index \. aspx </LookFor>
<SendTo> ~ /User/Index. aspx </SendTo>
</RewriterRule>

The second-level domain has been implemented here, no matter what Host header is entered (www. domain name. com and domain name. except for com, because these two domains have been bound to the primary site and are preferentially accessed to the primary site), abc can be implemented. domain name. com has accessed this user directory, and abc appears in the address bar of the browser. domain name. to ensure that the second-level domain accesses other pages and maintains the attributes of the second-level domain name, you also need to rewrite the URL at the same time, if you need to use the second-level domain Host Header (User Name) for other pages, you can obtain it from Session ["UserDomainName"]. For example, you need to file test in the User directory. aspx: displays the Host header name of the second-level domain, and the address in the browser address bar must be: abc. domain name. com/test. aspx, then in the web. add URL rewriting rules in config:

Code: <RewriterRule>
<LookFor> ~ /Test \. aspx </LookFor>
<SendTo> ~ /User/test. aspx </SendTo>
</RewriterRule>

Then, test. aspx displays the Host header name of the second-level domain by obtaining the value of Session ["UserDomainName"] or by URL or fetch.
Of course, you can also directly bind the second-level domain site to the User directory to obtain the Host Header (User Name) in the URL, but this may lose the convenience of data communication with the main site.

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.