Language: C#
URLRewriter Module C#
1: public class URLRewriterModule : IHttpModule 2: { 3: /// <summary> 4: /// You will need to configure this module in the web.config file of your 5: /// web and register it with IIS before being able to use it. For more information 6: /// see the following link: http://go.microsoft.com/?linkid=8101007 7: /// </summary> 8: #region IHttpModule Members 9: 10: public void Dispose() 11: { 12: //clean-up code here. 13: } 14: 15: public void Init(HttpApplication context) 16: { 17: // Below is an example of how you can handle LogRequest event and provide 18: // custom logging implementation for it 19: context.BeginRequest += new EventHandler(context_BeginRequest); 20: } 21: 22: private void context_BeginRequest(object sender, EventArgs e) 23: { 24: System.Web.HttpApplication app = (System.Web.HttpApplication)sender; 25: string requestedUrl = app.Request.Path.ToLower(); 26: string realUrl = GetRealUrl(requestedUrl); 27: if (!String.IsNullOrEmpty(realUrl)) 28: app.Context.RewritePath(realUrl, false); 29: } 30: 31: private string GetRealUrl(string requestedUrl) 32: { 33: // Implement your own logic here 34: 35: return GetRealPath(requestedUrl); 36: 37: } 38: 39: #endregion 40: 41: 42: public string GetRealPath(string requestedUrl) 43: { 44: string path = ""; 45: Dictionary<string, string> paths = GetPathsFromDatabase(); 46: if (paths.ContainsKey(requestedUrl)) 47: paths.TryGetValue(requestedUrl, out path); 48: return path; 49: } 50: 51: private static Dictionary<string, string> GetPathsFromDatabase() 52: { 53: Dictionary<string, string> paths = new Dictionary<string, string>(); 54: paths.Add("/Inicio", "/index.aspx"); 55: paths.Add("/Inicio/", "/index.aspx"); 56: 57: //paths.Add("/dev/Categorias/40/La-Ambientacion.aspx".ToLower(), "/dev/Subcategorias.aspx?CategoryID=40"); 58: //paths.Add("/Categorias/SecondSection/Default.aspx".ToLower(), "/URLRewrite/Default.aspx?SectionID=2"); 59: //paths.Add("/Categorias/FirstSection/Page1.aspx".ToLower(), "/URLRewrite/Default.aspx?SectionID=1&Item=1"); 60: //paths.Add("/Categorias/FirstSection/Page2.aspx".ToLower(), "/URLRewrite/Default.aspx?SectionID=1&Item=2"); 61: //paths.Add("/Categorias/SecondSection/Page1.aspx".ToLower(), "/URLRewrite/Default.aspx?SectionID=2&Item=1"); 62: //paths.Add("/Categorias/SecondSection/SubSection/AnotherOne/Page5.aspx".ToLower(), "/URLRewrite/Default.aspx?SectionID=2&Item=5"); 63: //paths.Add("/Quienes_Somos/".ToLower(), "/quienes_somos.aspx"); 64: //paths.Add("/dev/Quienes_Somos/".ToLower(), "/quienes_somos.aspx"); 65: return paths; 66: } 67: 68: }
Tags:
Description:
You can use this snippet to make an URL Rewriter full customizable module
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

