简体   繁体   中英

Rewrite URL in asp c#

How do I redirect url based on register client in c# .net or asp.net 4.0. For example if client registers as "client1" and our website is www.mycompany.com for every page client proceeds should get www.client1.mycompany.com.

More detailed example:

For example another client created is Client2. The pages i have created in general is like

 "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"

respectively I have searched on web and found for static pages or using Gloabal.asax during startup of application but haven't found any thing after user logged in.

There are several ways of doing so. ub1k stated on one way.

I think the easiest way is using the global.aspx.cs (If you don't have global.aspx then add it) and then

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");
}

Easy, clear and very powerful...

If you wish to make redirects based on client login - which is an internal application thing (cannot be handled by IIS - like with the IIS Url Rewriter) then you should probably create a HttpModule .

So what you should do is:

  • Create a module -> class that implements IHttpModule
  • In this module implement your redirecting logic
  • Plug it in, in web.config secion: <configuration><system.web><httpModules> like:

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

All can be found at: http://support.microsoft.com/kb/307996

Mind you kave to hook your logic to an event that is done after the authentication is done. I also believe that to read the user info you should have your module implement also IRequiresSessionState

    //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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM