In spite of extensive exposure to ASP.NET MVC I still tend to solve problems by first using ASP.NET Web Forms. Shallow criticisms aside, Web Forms provides a powerful mechanism for quickly creating complex user interfaces. That said, I still feel guilty about not pushing the MVC pattern just a little further when the opportunity arises. A valid criticisms of Web Forms development is its lack of support for test driven development, however, if you have started a Web Forms project and still want to integrate the MVC pattern, I would submit that ASP.NET Web API is one way to do that.

Web API

ASP.NET Web API is a framework that makes it easy to build building RESTful (uses the HTTP protocol) applications on the .NET Framework. Creating these services provides the opportunity to move code from “code behind” files into an API controller. Accessing the controller is done via AJAX which can allow for a more responsive UI. To start begin by installing the Web API package as follows:

Install-Package WebApi.All -Version 0.6.0

This install will allow you to Add->New Item->Web API Controller Class, and this is what you will get by default:

public class EmployeeController : ApiController
{
    // GET api/
    public IEnumerable Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api//5
    public string Get(int id)
    {
        return "value";
    }

    // POST api/
    public void Post([FromBody]string value)
    {
    }

    // PUT api//5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api//5
    public void Delete(int id)
    {
    }
}

The new controller shows off some of the coding by convention inherent to Web APIs, for example, the “Post” method will be called whenever a page is posted back to the server, to create other POST methods you can decorate said method with the [HttpPost] attribute. I will leave you to do the heavy lifting of deciding what code would be transferred to the API Controller and how you validate.

Routing

After creating your controller the next step is to create a routing rule that lets the application know which URIs are configured and what controllers will respond to those requests. In the following example routeTemplate we have defined a url that will redirect to our API Controller given the following format https://www.poppastring.com/api/Employee/1.

using System.Web.Routing;
using System.Web.Http;

namespace WebApplication1
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.MapHttpRoute(
                    name: "ActionApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { Controller = "Employee" }
                );
        }

With this URIs exposed it becomes a trivial task to start plugging AJAX directly into the webpages and calling these endpoints directly. Hopefully this kind after the fact change will assuage our collective Web Forms guilt.



Comment Section

Comments are closed.