【问题标题】:Url rewrite for subdomain as Username将子域的 URL 重写为用户名
【发布时间】:2013-05-11 01:00:46
【问题描述】:


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

用户名.域名.topdomain

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

domain.topdomain/用户名/profile.aspx

我们想要:

用户名.域名.topdomain/profile/

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

【问题讨论】:

  • 您是否真的获得了每个 username.domainname.topdomain 的 DNS 条目?
  • 不,没有 DNS 级别的条目。我们只需要伪造那个 url(我猜是虚拟路径??)。
  • 同意@DD59 我不明白如何在没有 DNS 处理的情况下实现这一目标
  • 所以这意味着我们不能在没有 DNS 处理的情况下伪造它,在这种情况下,每次用户向我们的网站注册时创建新的子域就太过分了,这是 asp.net 的一种方式可以在 DNS 上注册子域。我正在使用 Godaddy 和 Arvixe 共享主机
  • 您不需要为每个用户注册一个 DNS 子域。使用通配符子域 *.domainname.topdomain(请参阅 dofactory.com/topic/1410/…

标签: c# asp.net .net url-rewriting asp.net-4.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

【讨论】:

  • 你给你的邮件id。我将发送示例程序
  • 你好运行了你的代码,它只是简单的 urlrewrite 不是我正在寻找的,或者我应该说我正在寻找的一半。无论如何,谢谢它会帮助我减少我未来的编码:)
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-03
    • 2017-10-26
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多