TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ASP.NET CompleteRequest Method

This C# page uses the CompleteRequest method in ASP.NET. This method signals that the ASP.NET runtime should stop processing the request.

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf