Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

ASP.NET RewritePath Method

This ASP.NET tutorial uses C# code and calls RewritePath. RewritePath does internal redirects.

RewritePath handles internal redirects.

It is common to have page locations that are easy to remember and convenient. But we want to redirect them to a central place in the code. We rewrite paths in ASP.NET using the RewritePath method.

Note: You can change a page name into a query string. This allows for a simpler design.

Table. To rewrite paths, you need to know exactly what the request is for, and then what you must change it to. The purpose this rewriting accomplishes is that it allows all the code to be located in one file.

Path table

Path requested: /Content/ROT13.aspx
Rewritten path: default.aspx?file=rot13

Path requested: /Content/About.aspx
Rewritten path: default.aspx?file=about

Path requested: /Content/Array-Slice.aspx
Rewritten path: default.aspx?file=array-slice

Events. Rewriting is "internal" server-only redirecting. Global.asax is a useful file for your ASP.NET project—generally you should have one. It contains various event handlers. And to add a new event handler, we simply type it.

And: ASP.NET will find it and use it on its own. Add an Application_BeginRequest event handler.

Contents of Global.asax: C#

<%@ 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...
	// is set to InProc in the Web.config file....
	// or SQLServer, the event is not raised.

    void Application_BeginRequest(object sender, EventArgs e)
	// You typed this


This code shows the Global.asax file, which is a special file in the root of your application's directory. It will be used and applied automatically when the site runs. It contains the Application_BeginRequest method.

Auto-generated parts. The only part of the above file you must add is the Application_BeginRequest event. The rest will already be there. The signature for events has object and EventArgs arguments. It is important to include them.

BeginRequest. Application_BeginRequest receives each request that arrives at the ASP.NET application. It will receive requests for images, aspx files, and other files as people try to access those files on your server.

Next: We will use Application_BeginRequest to check for a certain kind of request, and then rewrite it.


Example. Here we add code to the Application_BeginRequest event handler that analyzes the HttpContent.Current path string. We detect if it needs to be rewritten, and if so, call RewritePath with the new path.

Note: The code above is run on each and every request that comes through ASP.NET.

So: You will want to make it relatively fast. The first thing it does above is get the current HttpContext.

Application_BeginRequest method: C#

void Application_BeginRequest(object sender, EventArgs e)
    HttpContext context = HttpContext.Current;
    string path = context.Request.Path.ToLower();

    int lastExtension = path.LastIndexOf(".aspx");
    if (lastExtension == -1)
    int lastContent = path.LastIndexOf("/content/");
    if (lastContent == -1)
    int lastSlash = path.LastIndexOf('/');
    string key = path.Substring(lastSlash + 1, (lastExtension - lastSlash - 1));

    context.RewritePath("~/default.aspx?file=" + key, false);

Using HttpContext.Current. This is a static property that returns the current "context" for the entire application. This means you can use the Request object anywhere, not just on a page file.

Rewriting logic. The code above references the request path. That's just a URL—we can treat it like any other string. We then use IndexOf. These method calls return the positions of the substrings in the path string.


And: We convert that to a query string, and pass in a virtual path to RewritePath.

Discussion. If this approach failed, you wouldn't be reading this article because you would be looking at a 404 error. This approach is a dynamic version of urlMappings. Use this sort of method for more complex requirements than urlMappings.


One small problem my implementation of this had when I applied it was that I needed to specify the second argument "false" to RewritePath. This bool indicates whether to rebase the client path.

Tip: If you specify false, the page the client ends up on will be kept the same as the request would indicate.

Summary. We rewrote paths in ASP.NET using the RewritePath method. Friendly URLs are valued more in search engine placement and are more likely to be bookmarked and shared than "ugly" or hard-to-read URLs.

So: Use URL rewriting in ASP.NET in your Global.asax file for a dynamic and easily modifiable mechanism.