TheDeveloperBlog.com


ASP.NET CompleteRequest Method

CompleteRequest is an HttpApplication instance method. It signals to the ASP.NET runtime that no further processing on the request is needed. It can be used on sites that do not use pages or ASHX files.


Example. First, this code snippet is part of the Global.asax file, which you can add by going to Add > Global Application Class. It uses the Application_BeginRequest event handler, which is run on every request.

Application_BeginRequest

We write a string to the response and then call CompleteRequest. When we call CompleteRequest, any page (such as WebForm1.aspx) that is browsed to will not be rendered. The request is terminated before that point.

Response.Write
Method that uses CompleteRequest: ASP.NET

using System;
using System.Web;

namespace WebApplication5
{
    public class Global : HttpApplication
    {
	protected void Application_BeginRequest(object sender, EventArgs e)
	{
	    // Write some content.
	    base.Response.Write("Write");

	    // Finish the request.
	    // ... No web forms will be used in the response.
	    base.CompleteRequest();
	}
    }
}

Text content of /WebForm1.aspx
    (Some markup was omitted.)

Test

Result of visiting /WebForm1.aspx

Write

If we remove the method call to CompleteRequest, the string "Write" and also the contents of /WebForm1.aspx will be written to the response. The page will have the text "Write" and "Test".

Note: The request will not be completed before the /WebForm1.aspx file is rendered.


Discussion. Why is CompleteRequest on the HttpApplication type important? One use for CompleteRequest is that you can implement your entire request processing logic inside BeginRequest and in other C# files.

You can actually avoid having any *.aspx files at all in your website. This can provide some performance benefits. As an aside, this site (Dot Net Perls) used to be implemented in this way.

Also: All requests are intercepted through Application_BeginRequest and then at the end of that method, CompleteRequest is called.

So: All pages are written from an in-memory cache and very little processing is done. The site treats pages and images in the same way.


Summary. CompleteRequest terminates all further processing after a certain point. You do not need to do anything further after CompleteRequest has been called and no *.aspx pages will be rendered after that point.