URLRewriting of a Pathname like (www.myspace.com/[username])
-
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
-
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
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: -
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:thanks for your answer, works great! first I've forgotten to add the extension for everything (*) to the IIS.