【问题标题】:Rewrite URL in asp c#在asp c#中重写URL
【发布时间】:2013-02-27 08:22:06
【问题描述】:

如何在 c# .net 或 asp.net 4.0 中根据注册客户端重定向 url。例如,如果客户注册为“client1”并且我们的网站是 www.mycompany.com,那么客户收益的每个页面都应该是 www.client1.mycompany.com。

更详细的例子:

例如,另一个创建的客户端是 Client2。我创建的页面一般是这样的

 "www.mycompany.com/product.aspx" 
 "www.mycompany.com/categories.aspx" should be shown as
 "www.client2.mycompany.com/product.aspx" and
 "www.client2.mycompany.com/categories.aspx"

分别 我在网上搜索并找到静态页面或在应用程序启动期间使用 Gloabal.asax,但在用户登录后没有找到任何东西。

【问题讨论】:

  • 参数在主机名中 - 只是为了检查一下,您是否已经配置了 DNS 以使其工作并使用在 IIS 中设置的相同网站/应用程序?
  • 那么希望根据用户被浏览的信息重定向每个请求?

标签: c# asp.net


【解决方案1】:

有几种方法可以做到这一点。 ub1k 以一种方式陈述。

我认为最简单的方法是使用 global.aspx.cs (如果你没有 global.aspx 然后添加它)然后

protected void Application_BeginRequest(object sender, EventArgs e)
{
    var currentPath = Request.Path.ToLower(); //get the request
    var myContext = HttpContext.Current;
    if (currentPath == "/addUser" || currentPath == "/newuser") //decide what to do with the request
        myContext.RewritePath("/login.ashx?path="+ currentPath);
     else  //default value
        myContext.RewritePath("/default.aspx");
}

简单、清晰且非常强大...

【讨论】:

  • +1 ..对我来说似乎是最简单的方法,而不是为此添加一个模块。
【解决方案2】:

如果您希望基于客户端登录进行重定向 - 这是一个内部应用程序的事情(不能由 IIS 处理 - 就像使用 IIS Url Rewriter 一样),那么您可能应该创建一个HttpModule

所以你应该做的是:

  • 创建一个模块 -> 实现IHttpModule的类
  • 在此模块中实现您的重定向逻辑
  • 插入,在 web.config 部分:<configuration><system.web><httpModules> 喜欢:

    <add name="MyModule" type="MyModule.Module, MyModule" />

所有内容都可以在以下位置找到:http://support.microsoft.com/kb/307996

请注意,您必须将您的逻辑与已完成的事件挂钩 after 身份验证已完成。 我也相信要阅读用户信息,您还应该让您的模块实现IRequiresSessionState

【讨论】:

    【解决方案3】:
        //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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      • 2012-08-03
      • 2011-10-23
      • 2012-03-18
      • 2016-05-23
      相关资源
      最近更新 更多