Website two domain name uses ASP.NET 2.0 the realization plan

Source: Internet
Author: User
Tags config empty header httpcontext iis split tostring root directory
Asp.net| level Two domain name I understand that there are two options, there may be its way, I hope we have a lot of discussion! Basic ideas:
1. Domain name support for the pan-resolution, that is to refer to: A record *.xrss.cn resolution to the server IP, server IIS to do bindings, binding the host head is empty;
2. In order to achieve complete level two domain, build two sites, one for the main station, one for users, two sites directory refers to a directory of the same site
3. In the Web program or take the URL source of the level two domain name host head, such as: abc.xrss.cn in the ABC;
4. With the acquisition of the two-level domain name, stored in session, convenient access
5. Use the obtained level two domain name, rewrite the address with the URL

Implementation method:
Domain A record parsing needless to say, is to do a *.xrss.cn a record resolution to your server IP

Method One: Two level domain name URL to turn
A. Setting up a site, binding the domain name in IIS (WIN2003-IIS6)
Open IIS, right-click the site, and then properties, click on the Advanced button of the site item IP address, then edit or add the new binding, the host header value is empty.

Below to obtain the URL address for analysis, the following is the procedure code, used to obtain and determine the host header, the code is placed in the default document in the first file Index.aspx.cs

Code:
       ///<summary>
        ///get two domain host header value and implement turn to
       ///</summary>
         public void Checkdomain ()
        {
             HostName = HttpContext.Current.Request.Url.Host.ToString (); Get URL host address
            userhost = hostname.split (new Char[] {'. '});  //Array to "." Separate

Determine whether the two-level domain name address conforms to abc.xrss.cn this format, and the array userhost length is not greater than 3, otherwise jump to other pages
if (Userhost.length > 3)
{
HttpContext.Current.Response.Redirect ("http://www.xrss.cn/Error.aspx"); Jump to error page
Return
}

UserDomainName = userhost[0].    ToString (); Gets the first set of values in the array, and the two-level domain name host header

Make a specific judgment, and do not use the main header as a two-level domain name
if (userdomainname.tolower () = = "www" | | Userdomainname.tolower () = = "Domain Name" | | UserDomainName = = NULL | | userdomainname.tostring () = = "")
{
Your moves.
}
else {
HttpContext.Current.Response.Redirect ("/user/"); Jump to the user directory, that is, a level two domain name to go to the directory, of course, you can also jump to *.aspx? Userid=xxx such a link
Return
}

}


Here already can realize abc.xrss.cn jump to the specified page or link, but not the real level two domain name, but only the URL to turn.


Method Two: The real level two domain name
A. Setting up a site
At this point we need to establish two sites, a primary site, a level two domain site, two sites of the same directory directory, and its directory contains Default.aspx and index.aspx two files. The method is established as follows:
A). Main site Establishment method reference method one of the site is established, however, the host header is not empty, need to be set to www.xrss.cn and xrss.cn, of course, you can also set other do not want to do two domain host head. Site default Access document is: Default.aspx.
B. Level two domain site establishment method is the same as the site in method one, and its default access document is: Index.aspx.

B. Now we're going to use the level two domain site we built before, and we'll put the user's data in the users directory under the root directory.
The following is the/USER directory in the default document (Index.aspx.cs) process code, the main purpose is to two-level domain name host to the session, easy to call, but also two domain (user area) of the first file

Code:
       ///<summary>
        ///gets the two domain host header value, which is stored in session["UserDomainName"
       ///</summary
        public void userdomainnamesession ()
         {
            HostName = HttpContext.Current.Request.Url.Host.ToString ();       //Get URL host address
            userhost = Hostname.split (new char[] {'. '});        //Array to "." Separate

                        //Judge whether the two-level domain name address conforms to abc.xrss.cn this format, and the array userhost length is not greater than 3, otherwise jumps to the other page
             if (Userhost.length > 3)
             {
                 HttpContext.Current.Response.Redirect ("http://www.xrss.cn//Error.aspx");        //Jump to error page
                                  return;
           }

UserDomainName = userhost[0].        ToString (); Gets the first set of values in the array, and the two-level domain name host header

Make a specific judgment, and do not use the main header as a two-level domain name
if (userdomainname.tolower () = = "www" | | Userdomainname.tolower () = = "Domain Name" | | UserDomainName = = NULL | | userdomainname.tostring () = = "")
{
Your moves.
}
Else
{
httpcontext.current.session["UserDomainName"] = UserDomainName; The two-level domain name host header is stored in session
}
}

Your treatment of session["UserDomainName", such as the value of this session["UserDomainName" is "abc", then you can index.aspx? USERNAME=ABC, if you do not want to use session, you can use the two-level domain host header, and then through the URL address to get.

c.        URL Rewrite
I use Microsoft's Urlrewriter, use the method see: http://www.microsoft.com/ The overriding method in China/msdn/library/webservices/asp.net/urlrewriting.......g.mspx?mfr=true
Web.config is:
                          <!--User area host header URL Rewrite, to achieve when abc.xrss.cn access to the site, by the level two domain site, the default first file for index.aspx, index.aspx address rewrite to/user /index.aspx-->


Code: [Copy to clipboard]
                         <rewriterrule>
                                  <lookfor>~/index\.aspx</lookfor>
                                  <sendto>~/user/index.aspx</sendto
                         </rewriterrule>


The level two domain has been implemented here, regardless of the type of host header (except www.xrss.cn and xrss.cn, which are already bound to the primary site, with priority access to the primary site), you can implement the abc.xrss.cn access to this user directory, and the browser address bar, appears to be abc.xrss.cn such a domain name address, in order to ensure that the level two domain access to other pages also maintain a level two domain name properties, also need to do URL rewriting, in other pages such as two domain host header (user name ), from session["UserDomainName"], for example, to file test.aspx in the user directory, display a level two domain host header name, while maintaining the browser address bar address: abc.xrss.cn/test.aspx, Then add the URL rewrite rule in web.config:


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

Test.aspx itself then displays the level two domain host header name by obtaining the value of session["UserDomainName" or by URL or fetch.
Of course, you can also directly bind a level two domain site to this user directory, to get the host header (username) in the URL, but this may lose the convenience of communication with the master station data.



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.