2013-05-16 47 views
0


首先,我没有足够的经验在Url重写在asp.net中。反正我拉网址重写感谢SO和其他有用的文章通过互联网。但是,这个新项目有一个特定的需要,我必须表明网址为不同的用户为:Url重写为子域名为用户名

username.domainname.topdomain

进一步为用户的特定页面的每一个动作应该这样工作,即对而不是用户profile.aspx页:

domain.topdomain /用户名/ profile.aspx

我们婉T:

username.domainname.topdomain /资料/

如何我拉了这一点?
Regards
P.S.该请求可以具有“N”个查询字符串,所以web.config中的任何内容都会太复杂以至于无法处理。

+1

你真的有每个username.domainname.topdomain的DNS条目? – inspite

+0

否,没有DNS级别条目。我们只需假冒该网址(如我猜想的虚拟路径)。 – Alok

+0

同意@ DD59我不明白如何实现这个没有DNS处理 – jbl

回答

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 
+0

你能解释一下吗? – Alok

+0

你给你的邮件id.i将发送示例程序 – karthi

+0

[email protected] – Alok

0

Asp.Net中的HttpContext类无法帮助您处理此问题。其中的重写器方法旨在覆盖路径和查询字符串,而不是主机名或端口。
我相信你需要使用客户端重写来改变主机名:发送一些脚本到客户端从不同的位置重新加载。

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