CodeBehind 4.7 introduces an important improvement to the way Form data is handled in incoming requests.
From this version onward, Form data should no longer be accessed directly through context.Request.Form. The internal structure responsible for injecting Form data into every request has been removed from the CodeBehind core.
Previously, developers could use conditions such as:
if (context.Request.Form["Button"].Has())
Button_Click(context);
However, this approach is no longer supported. If a request does not contain Form content, attempting to access context.Request.Form will cause an error. For example, if the incoming request is a GET request, the condition above attempts to read Form data from a request that does not contain Form content.
To prevent this issue, Form data should only be accessed after verifying that the request contains Form content through HasFormContentType.
CodeBehind 4.7 also provides the IfForm extension method as a simpler and safer solution. It allows developers to conditionally access Form data without directly attempting to read Form values from requests that do not contain Form content.
The previous code can therefore be replaced with:
if (context.Request.IfForm()["Button"].Has())
Button_Click(context);
The IfForm extension method provides a safer way to work with Form data and prevents unnecessary attempts to read Form values from requests that do not contain Form content.
This improvement also reduces unnecessary processing inside the CodeBehind core. Since the Form data injection structure has been removed from the CodeBehind core, requests that do not require Form processing, such as GET requests, are no longer subject to unnecessary Form-related processing.
This approach is particularly important because CodeBehind is designed around processing incoming requests through the PageLoad method in the controller. In the recommended CodeBehind development pattern, submitted Form data is examined to identify the type of request, and the appropriate operation or event is executed based on the available data.
For this reason, using conditions based on Form data is one of the primary approaches for handling different requests within a controller. With IfForm, these checks can be performed safely, allowing requests such as GET requests, which do not contain Form content, to be processed without causing an error.
For example, the PageLoad method can handle requests associated with different controls based on their Form data:
public void PageLoad(HttpContext context)
{
if (context.Request.IfForm()["run_test1"].Has())
{
Button1_Click(context);
return;
}
if (context.Request.IfForm()["run_test2"].Has())
{
Button2_Click(context);
return;
}
if (context.Request.IfForm()["run_test3"].Has())
{
Button3_Click(context);
return;
}
WebForms form = new WebForms();
form.SetBackgroundColor("<main>", "lightblue");
form.AddText("<main>", Fetch.LoadUrl("/contact"));
Write(form.ExportToHtmlComment());
}
In this structure, Form requests are checked first. If the request contains the data associated with one of the operations, the corresponding method is executed and request processing ends. If the request does not correspond to any of these operations, execution continues through the PageLoad method and the normal page content is processed.
This pattern provides a simple, clear, and controlled structure for handling different types of requests within a single controller.
The new approach provides a cleaner and more controlled way to process incoming requests while making Form data access safer and simpler for developers.
Top comments (0)