Url将子域重写为用户名

时间:2013-05-16 08:16:55

标签: c# asp.net .net url-rewriting asp.net-4.0


首先,我没有足够的经验在asp.net重写Url。无论如何我感谢网上的SO和其他有用的文章重写网址。但是这个新项目有一个特定的需求,我必须为不同的用户显示Url:

  

username.domainname.topdomain

用户特定页面的每个操作都应该像这样工作,即用户profile.aspx页面而不是:

  

domain.topdomain /用户名/ profile.aspx

我们想要:

  

username.domainname.topdomain /简档/

我怎么能把它拉出来?
问候
附:请求可以有“N”个查询字符串,因此web.config中的任何内容都太复杂而无法处理。

2 个答案:

答案 0 :(得分:1)

    //DLL: Intelligencia.UrlRewriter.dll    

    //web.config
    <configuration>

      <configSections>
        <section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter"/>
      </configSections>
    </configuration>

    <system.web>
        <httpModules>
          <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
        </httpModules>
    </system.web>


    <rewriter>
        <rewrite url="~/(.+)/CompanyHomePage" to="~/Home.aspx"/>
    </rewriter>


    //C#:

     string strTitle = Session["company_name"].ToString();
     strTitle = strTitle.Trim('-');
     strTitle = strTitle.ToLower();
     char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();
     strTitle = strTitle.Replace("c#", "C-Sharp");
     strTitle = strTitle.Replace("vb.net", "VB-Net");
     strTitle = strTitle.Replace("asp.net", "Asp-Net");
     strTitle = strTitle.Replace(".", "-");

    for (int i = 0; i < chars.Length; i++)
    {
        string strChar = chars.GetValue(i).ToString();
        if (strTitle.Contains(strChar))
        {
           strTitle = strTitle.Replace(strChar, string.Empty);
        }
    }
     strTitle = strTitle.Replace(" ", "-");
     strTitle = strTitle.Replace("--", "-");
     strTitle = strTitle.Replace("---", "-");
     strTitle = strTitle.Replace("----", "-");
     strTitle = strTitle.Replace("-----", "-");
     strTitle = strTitle.Replace("----", "-");
     strTitle = strTitle.Replace("---", "-");
     strTitle = strTitle.Replace("--", "-");
     strTitle = strTitle.Trim();
     strTitle = strTitle.Trim('-');

     Response.Redirect("~/" + strTitle + "/CompanyHomePage", false);//Redirect to ~/Home.aspx page


//reference: CompanyHomePage same in web.config  <rewrite url="~/(.+)/CompanyHomePage" to="~/Home.aspx"/> which company is log in to sight that company name show in url like this http://abcd//CompanyHomePage

答案 1 :(得分:0)

Asp.Net中的HttpContext类不会帮助你解决这个问题。其上的重写器方法旨在覆盖路径和查询字符串,而不是主机名或端口 我相信您需要使用客户端重写才能更改主机名:将一些脚本发送到客户端以从其他位置重新加载。

正如其他人所说,您需要设置一些通配符DNS设置,以允许您的username.domain.tld主机名解析为您的网络服务器。