Url rewriting in asp.net c#  

by ne on 2021-09-29 under C#/Php

This tutorial shows you how to rewrite URL paths on-the-fly using ASP.NET 2.0 and C# .NET. Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services.

Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team. In some instances you may want to rewrite a URL on-the-fly in ASP.NET. This is done in the Global.asax file which contains event handlers for major events like Application_Start and Session_Start.

This file is located in the root of the application directory and handles application level logic that doesnt interact with or create UI objects. In this example, we will be writing code for the Application_BeginRequest to rewrite the URL before any resource or URL is activated in the application. To perform any kind of advanced URL rewriting you will need to use the RegularExpression class objects found in System.Text.RegularExpressions.

Whenever a new HTTP request starts the Application_BeginRequest() event fires and we grab the current request in a HTTPContext object. HttpContext myContext = HttpContext.Current; By accessing the Request property of this object we can study an HttpRequest object and determine information commonly included in most HTTP requests (such as the URL requested). In this example, we want to redirect any request for "Default.aspx" and rewrite it to "Something.aspx". We start by creating a Regex object that will capture the appropriate text.

Regex rewrite_regex = new Regex(@"(.+)\/((.+)\.aspx)", RegexOptions.IgnoreCase);

This regular expression has three capturing groups: the directory the file is in, the filename (without extension), and the filename (with extension). We test the last group (filename w/ extension) to make sure it matches "Default.aspx" before we use the RewritePath() function of our HttpRequest object and forward the user before any page has been loaded.

try
{

//see if we need to rewrite the URL
Match match_rewrite = rewrite_regex.Match(myContext.Request.Path.ToString());

if (match_rewrite.Groups[2].Captures[0].ToString() == "Default.aspx")
{

myContext.RewritePath("Something.aspx", true);
}


The flow for the entire Global.asax page is as follows. Some event stubs are created by default when adding a new Global.asax file via Visual Studios.

These are included for completeness. If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

<%@ Application Language="C#" %>

<script runat="server">

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup

}

void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown

}

void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs

}

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started

}

void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.

}

protected void Application_BeginRequest(object sender, EventArgs e)
{

HttpContext myContext = HttpContext.Current;

Regex rewrite_regex = new Regex(@"(.+)\/((.+)\.aspx)", RegexOptions.IgnoreCase);

try
{

//see if we need to rewrite the URL
Match match_rewrite = rewrite_regex.Match(myContext.Request.Path.ToString());

if (match_rewrite.Groups[2].Captures[0].ToString() == "Default.aspx")
{

myContext.RewritePath("Something.aspx", true);

}
} catch (Exception ex) { } }
  </script>

Thanks ..