Posted by & filed under Web Development.

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

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

  1. Andrews

    Hello. I was directed to this pstoing from the DNN forum. I was trying to find out if there a way to assign different skins to the user profile page, based on the user’s role group? For example, we would like users in role group 1 to have their user profile pages using skin 1 and users in role group 2 to have their user profile pages using skin 2.We have 1 portal with multiple sites each site is branded uniquely and has its own set of menus, etc. site 1 uses skin 1, site 2 uses skin 2, etc.This is why we would like to have the profile pages of the users that belong to different sites to have the same skins as the sites they belong to.Just to reiterate, we are hosting these different sites under one portal.Would your solution work for our needs? I’m a DNN newbie and any help you can provide will be tremendous. Thank you.

Leave a Reply

  • (will not be published)