Tag Archives: Web Forms

Invoke an action between ASP.Net pages. Create a simple REST API for an ASP.NET page

Update March 1, 2013 I didn’t have a full understanding of a REST service at the time I wrote this post. I have a better understanding now and recommend using ASP.NET Web API

I needed a simple way for one page to call an action in another page.  In the spirit of a RESTful API I wanted that interaction to be called through a simple URL. 

http://localhost/Default.aspx?action=save

I initially started using a switch statement to handle the action passed in on the QueryString, but really wasn’t happy with the result.  I wasn’t interested in having to maintain a switch statement for the actions.  My first pass looked like this:

protected string Action { get; set; }

protected override void OnInit(EventArgs e)
{
    Action = Request.QueryString["action"] ?? string.Empty;
    base.OnInit(e);
}

protected void Page_Load(object sender, EventArgs e)
{
    switch (Action)
    {
        "Save":
            SaveAction();
            break;
        "Load":
            LoadAction();
            break;
        default:
            break;
    }
}

private void SaveAction()
{
    Response.Write("Save action called.");
}

private void LoadAction()
{
    Response.Write("Load action called.");
}

I decided to break out the handling of the action into a base class called ActionPage and use a event model to call the actions methods.  Following this refactoring the code behind was much easier to follow.

public partial class _Default : ActionPage
{
    protected void Page_Load(object sender, EventArgs e)
    { }

    protected override void RegisterActions()
    {
        PageActions.Add("save", SaveAction);
        PageActions.Add("load", LoadAction);
    }

    private void SaveAction()
    {
        Response.Write("Save action called.");
    }
    private void LoadAction()
    {
        Response.Write("Load action called.");
    }
}

The key was creating a ‘PageAction’ delegate that could be registered for each possible page action.  The RegisterActions method shows the action methods being registered using the PageAction delegate.  The page is now only responsible for performing the actions and no longer has to determine what action to call.   The work of reading the action from the QueryString and calling the action have been placed into the ActionPage base class.

public class ActionPage  : System.Web.UI.Page
{
    protected delegate void PageAction();
    protected readonly IDictionary<string, PageAction> PageActions = new Dictionary<string, PageAction>();
    protected string Action { get; set; }

    public ActionPage()
    {
        RegisterActions();
    }

    protected virtual void RegisterActions()
    {
    }

    protected void HandleAction(string action)
    {
        if (PageActions.ContainsKey(action))
            PageActions[action]();
    }

    protected override void OnInit(EventArgs e)
    {
        Action = Request.QueryString["action"] ?? string.Empty;
        base.OnInit(e);
    }
    protected override void OnLoad(EventArgs e)
    {
        HandleAction(Action);
        base.OnLoad(e);
    }
}

The ActionPage starts by calling RegisterActions in the constructor.  Register actions is the pages chance to register actions with the ActionPage.  The actions is then pulled from the QueryString it the OnInit method.  Then in OnLoad the action matching the one passed on the query string is called.  In this model if an invalid action is called nothing happens and the page renders normally.

SimpleRESTAPI_SampleSource.zip