Answered by:
implementing rewrite for http://site/NICK => http://site/profile.aspx?nick=NICK

Question
-
User1882076431 posted
hey guys, like the title says, how can i accomplish this sort of a rewrite as simple as possible? is it with httphandlers or perhaps the asp.net routing? i am using version 3.5.
the problem is that i want any other requests to be ignored, like requests to .aspx files, images, folders and such.
your suggestions are appreciated :)
Tuesday, July 28, 2009 2:15 PM
Answers
-
User-1739576956 posted
I think this will do the job:
namespace Handlers { using System; using System.Web; using System.Text.RegularExpressions; using System.Web.UI; public class MyHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { String virtualPath = context.Request.Path; //if the virtual path contains ".aspx", then redirect to the real page using //without using this custom http handler if (context.Request.Path.Contains(".aspx")) { IHttpHandler handler = PageParser.GetCompiledPageInstance(context.Request.Path, context.Request.PhysicalPath, context); context.Server.Transfer(handler, true); } //parse the site name from the virtual path, e.g. /WebSite/Test or /WebSite/Test/ String site = Regex.Match(virtualPath, "/(?<site>[^/]*?)/?$", RegexOptions.IgnoreCase).Groups["site"].Value; context.Response.Redirect("~/profile.aspx?nick=" + site); } public bool IsReusable { get { return false; } } } }
The web.config is still just<add verb="*" path="*" type="Handlers.MyHandler,Handlers" />
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 28, 2009 6:56 PM
All replies
-
User-1739576956 posted
You could use HttpHandlers - write a handler smth like:
namespace Handlers { using System; using System.Web; using System.Text.RegularExpressions; public class MyHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { String site = Regex.Match(context.Request.Path, "/(?<site>[^/]*?)/$", RegexOptions.IgnoreCase).Groups["site"].Value; context.Response.Redirect(context.Server.MapPath("~/") + "profile.aspx?nick=" + site); } public bool IsReusable { get { return false; } } } }
Tuesday, July 28, 2009 3:14 PM -
User1882076431 posted
Thanks Maate, i will see if this will help, but then will this have to process all requests, in web.config i would put:
<add verb="*" path="*" type="MyHandler"/>
If so, how do we make it so it ignores everything else but the requests to /NICK ?Thanks for your help :)
Tuesday, July 28, 2009 3:25 PM -
User-1739576956 posted
I think there's actually different options.
First, you could require user to postfix request with "/", e.g. http://yoursite.com/NICK/. This could be done registerings the handler like
<
add verb="*" path="*/" type="Handlers.MyHandler,Handlers" />
With this, requests to "/NICK" would be ignored by handler.
Another option would be to follow your own suggestion, i.e. setting path="*". Then you would have to parse the url in the handler - e.g. smth like if(Regex.Match(context.Request.Path, @".*\.*").Success) throw new SomeRelevantException(); If you follow this approach you need to rewrite my original regex a bit - out of memory: "/(?<site>[^/]*)/?$".
Br. Morten
Tuesday, July 28, 2009 4:18 PM -
User1882076431 posted
thanks, i dont think i can require postfix of "/" so it only gives me second option.
i am having really hard time figuring out once i determine that the URL is not a NICK how to let the httphandler process any other requests "as is". i'm very new into this http rewriting world of asp.net :) any suggestions?
Tuesday, July 28, 2009 4:43 PM -
User1882076431 posted
i dont know if i expressed myself clear so i will give examples:
i want these to get handled:
http://site.com/john => http://site.com/profile.aspx?nick=john
http://site.com/mark => http://site.com/profile.aspx?nick=mark
http://site.com/fred => http://site.com/profile.aspx?nick=fredi DONT want these to get handled:
http://site.com/page.aspx
http://site.com/images/logo.jpg
etc...thanks
Tuesday, July 28, 2009 4:53 PM -
User-1739576956 posted
I think this will do the job:
namespace Handlers { using System; using System.Web; using System.Text.RegularExpressions; using System.Web.UI; public class MyHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { String virtualPath = context.Request.Path; //if the virtual path contains ".aspx", then redirect to the real page using //without using this custom http handler if (context.Request.Path.Contains(".aspx")) { IHttpHandler handler = PageParser.GetCompiledPageInstance(context.Request.Path, context.Request.PhysicalPath, context); context.Server.Transfer(handler, true); } //parse the site name from the virtual path, e.g. /WebSite/Test or /WebSite/Test/ String site = Regex.Match(virtualPath, "/(?<site>[^/]*?)/?$", RegexOptions.IgnoreCase).Groups["site"].Value; context.Response.Redirect("~/profile.aspx?nick=" + site); } public bool IsReusable { get { return false; } } } }
The web.config is still just<add verb="*" path="*" type="Handlers.MyHandler,Handlers" />
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 28, 2009 6:56 PM