Let your asp.net virtual host also support subsites

Source: Internet
Author: User
Tags httpcontext root directory

How to make the binding of these several domain names open the page (that is, the function of the subsite)? In fact, it's simple, just 4 steps:

1 bind several domain names to the virtual host; for example: www.bianceng.cn,services.abc.com,support.abc.com.

2 Create several folders under the root directory of the virtual host site; For example: Services,support;www folder is not built.

3 under vs. a Web project, create the same folders, add a Default.aspx file under each folder; for example: Services,support.

4 Add Application_BeginRequest event in Global.asax:

protected void Application_BeginRequest(object sender, EventArgs e)

{
      string sumDomain;
      string domain = Request.Url.Host;
      // http://localhost 可没有“.”啊
      int i = domain.IndexOf('.');
      if (i > 0)
      {
        // 取域名(例如,www.abc.com)第一个“.”之前的部分(不包括第一个“.”)
        sumDomain = domain.Substring(0, i);

// 如果不是“www”,则自动转向到 http://www.abc.com/xxx,
        //  地址栏的URL,不会显示http://www.abc.com/xxx,而是显示http://xxx.abc.com
        if (sumDomain.IndexOf("www") == -1)
        {
          // 注意,这一句是关键
          HttpContext.Current.RewritePath("~/" + sumDomain + Request.Url.PathAndQuery);
        }
      }
    }

How, is not very simple! Haha, this is the URL rewrite (HttpContext.Current.RewritePath).

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.