HTTP objects

WebMap Http Objects are a set of classes and objects created to wrap and adapt HTTP operations that were natively supported in WebForms. For this, firstly, we should understand a little about them.

You should have some knowledge in the following areas:

  • HTTP Objects

  • Cookies

Overview

This documentation is intended for someone who is familiar with programming concepts and ASP.NET Web Forms, but new to WebMap.

Request Headers

A request header is an HTTP header in charge of providing context information about the current request been executed. Its main purpose is to allow adapting the response from the server.

To access the request header data, we must first understand how the flow of a native application is established in the client-server process of Webforms and how these parameters are passed during communication.

The following diagram describes exactly the procedure described.

HTTP Request

When an user requests a page, it is compiled and executed on the server by WebMap, then it is wrapped and adapted from an AspNetCore.Http.HttpRequest object into a Mobilize.Web.UI.HttpRequest object which is accessible by the instance of page class in the converted code.

During the execution of an aspx.cs file, also known as a page, which is requested from the client, it receives within a context the current data of the request, response and the session state as composition relation properties of the instance of the page.

In code, the HttpRequest object stored in the Page.Request object which is used to access data from the request header. In this object you can find the Request.ServerVariables["Key"] property which queries the state of a header variable. The ServerVariables is a collection of strings that are only exposed through Request.

The following example demonstrates how to retrieve a query string value when loading a page.

[Observable]
public partial class DummyPage: Mobilize.Web.UI.Controls.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string referer = Request.ServerVariables["HTTP_REFERER"];
        return;
    }
}

Http Response

Similar to the request object, WebMap also wraps and adapts the AspNetCore.Http.HttpResponse object into the Mobilize.Web.UI.HttpResponse class in which its methods and properties are exposed through an adapter when page is loaded.

The following members of the HttpResponse class are supported on WebMap.

  • Buffer

  • Cache

  • Charset

  • ContentEncoding

  • ContentType

  • AddHeader()

  • AppendHeader()

  • BinaryWrite()

  • Clear()

  • ClearHeaders()

  • End()

  • Flush()

  • Redirect()

  • TransmitFile()

In code, the HttpResponse object stored in the Page.Response object which is used to access data from the response header. Members such as the Clear method can be accessed in WebMap just by using the Response.Clear(), which wraps the ASP HttpResponse.Clear method.

The following example sets the response's ContentType property to image/jpeg and also calls the Clear method to remove other content that might have been attached to the response previously.

Response.ContentType = "image/jpeg";
Response.Clear();

Another example of an use of this property is to specify a redirect action which will be in charge of loading a given page.

private void btnSubmitDeal_Click(object sender, EventArgs e)
{
    Response.Redirect("MADB.SubmitDeal");
}

Redirect from HttpResponse object cannot abort the current thread, so, we store only the last committed page to be redirected. At the end of the request, the “CommitContextCommand” should redirect to the last redirection.

Cookies

Setting a cookie and reading on WebMap is a simple procedure. In this way, you can create a cookie in the server and add it to the request:

Request.Cookies.Add(new Http.HttpCookie() {
    Name = "user_id",
    Value = "1",
})

Http.HttpCookie is a class that allows us to create a candidate object that will become a cookie and then added into cookies collection.

The following example demonstrates how to get a value user_id from a cookie.

var userId = Request.Cookies["user_id"];

Notice how the request is used when you what to read it back, it means that you are pulling information from a request made by the client.

Once the request is completed, the cookies are sent to the browser to later be used in a next request if they are requested.

To authenticate a user via cookie, you can use the FormsAuthentication.SetAuthCookie method to encrypt and register an authentication cookie. In this case, the cookie will be added into cookie collection of the current request.

This authentication method takes two parameters, the name of user that is being logged in, if whether or not the cookie will persistent or not.

The following example demonstrates how to register an authentication cookie.

FormsAuthentication.SetAuthCookie(“JSmith”, true);

Since ASP .NET Core does not support the Web.config file and the App.config file cannot load the System.Web namespace, all configuration options for the authentication cookie should be moved to the appSetting.config file.

In order to configure it, you should use the full qualified name as the key to the item of the app setting, see the following example:

<add key="system.web.authentication.forms.name" value=".ASPXFORMAUTH"/>
<add key="system.web.authentication.forms.timeout" value="2"/>

Current the conversion of the System.Web.Authentication.Forms.Name and the System.Web.Authentication.Forms.Timeout namespaces are the only ones supported as authentication options.

Last updated