Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. URLRewriting of a Pathname like (www.myspace.com/[username])

URLRewriting of a Pathname like (www.myspace.com/[username])

Scheduled Pinned Locked Moved ASP.NET
questioncom
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Succodimele
    wrote on last edited by
    #1

    Hello I've posted the question already, but i get no answer. Maybe it was not clear, what's my goal. Thats why I ask again (a bit different;)). I'm sure most of you know myspace.com. There it's possible to visit a userprofile directly over doaminname/username (e.g. http://www.myspace.com/username). I want to build something like this for my own website. I red about URLRewriting, to change the URL into the global.asax (Application_BeginRequest). But this is only possible, when the Directory whith the specific username exists, and into it, there have to be a default.aspx File. Is this right? Do I really have to create all these Directories and Files? Its not a big work, but it doesn't look really nice. I thought also about the possibility to change the 404Error File into an .aspx File and handle it there. Is this the solution? Or which is the right/professional way? Best regards succo

    E 1 Reply Last reply
    0
    • S Succodimele

      Hello I've posted the question already, but i get no answer. Maybe it was not clear, what's my goal. Thats why I ask again (a bit different;)). I'm sure most of you know myspace.com. There it's possible to visit a userprofile directly over doaminname/username (e.g. http://www.myspace.com/username). I want to build something like this for my own website. I red about URLRewriting, to change the URL into the global.asax (Application_BeginRequest). But this is only possible, when the Directory whith the specific username exists, and into it, there have to be a default.aspx File. Is this right? Do I really have to create all these Directories and Files? Its not a big work, but it doesn't look really nice. I thought also about the possibility to change the 404Error File into an .aspx File and handle it there. Is this the solution? Or which is the right/professional way? Best regards succo

      E Offline
      E Offline
      e2canoe
      wrote on last edited by
      #2

      No part of the path needs to exist (except for the domain of course). Here's some code from a URLRewriter I wrote recently (it sets UICulture based on a url) that hopefully gives you enough hints on how to do this. If you still have questions, send me an email at support [at] stratelogics [dot] com and I'll help you out. public class URLRewriterHttpModule : IHttpModule { public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); } public void Dispose() { // TO DO: Dispose resource if required } private void context_BeginRequest(object sender, EventArgs e) { HttpRequest request = ((HttpApplication)sender).Request; HttpContext context = ((HttpApplication)sender).Context; string applicationPath = request.ApplicationPath; if (applicationPath == "/") { applicationPath = string.Empty; } string requestPath = request.Url.AbsolutePath.Substring(applicationPath.Length); LoadCulture(ref requestPath); context.RewritePath(applicationPath + requestPath); } private void LoadCulture(ref string path) { if (path.Contains("_e.aspx")) { // Set culture to English and remove _e from filename Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA"); path = path.Replace("_e.aspx", ".aspx"); } else { if (path.Contains("_f.aspx")) { // Set culture to French and remove _f from filename Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA"); path = path.Replace("_f.aspx", ".aspx"); } else { // Leave culture as is and don't modify filename } } // Set the UICulture to match the Culture Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; } } Dont forget to put this in your web.config's system.web section:

      S 1 Reply Last reply
      0
      • E e2canoe

        No part of the path needs to exist (except for the domain of course). Here's some code from a URLRewriter I wrote recently (it sets UICulture based on a url) that hopefully gives you enough hints on how to do this. If you still have questions, send me an email at support [at] stratelogics [dot] com and I'll help you out. public class URLRewriterHttpModule : IHttpModule { public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); } public void Dispose() { // TO DO: Dispose resource if required } private void context_BeginRequest(object sender, EventArgs e) { HttpRequest request = ((HttpApplication)sender).Request; HttpContext context = ((HttpApplication)sender).Context; string applicationPath = request.ApplicationPath; if (applicationPath == "/") { applicationPath = string.Empty; } string requestPath = request.Url.AbsolutePath.Substring(applicationPath.Length); LoadCulture(ref requestPath); context.RewritePath(applicationPath + requestPath); } private void LoadCulture(ref string path) { if (path.Contains("_e.aspx")) { // Set culture to English and remove _e from filename Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA"); path = path.Replace("_e.aspx", ".aspx"); } else { if (path.Contains("_f.aspx")) { // Set culture to French and remove _f from filename Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA"); path = path.Replace("_f.aspx", ".aspx"); } else { // Leave culture as is and don't modify filename } } // Set the UICulture to match the Culture Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; } } Dont forget to put this in your web.config's system.web section:

        S Offline
        S Offline
        Succodimele
        wrote on last edited by
        #3

        thanks for your answer, works great! first I've forgotten to add the extension for everything (*) to the IIS.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups