Telerik blogs
Planning an ASP.NET Web Forms Application Controllers and Testing_870x220

In spite of the many choices for web developers today, ASP.NET Web Forms is alive and well. One of the platform’s many strengths is its long history of stability. Microsoft has kept it current with frequent updates and the .NET Framework 4.7.1 continues to add features that can be utilized by Web Forms applications.

So what does that mean to you? If you are getting ready to build an ASP.NET Web Forms application, what should you consider?

My colleague, Ed Charbeneau, shares his thoughts and recommendations in the “Planning a Modern Web Forms Application” whitepaper. In this post, we’ll look at two excerpts from that paper – Using Controllers and Routing in Web Forms and Testing Methodologies. He explores the ability to take advantage of web API controller architecture without switching to ASP.NET MVC and he shares his thoughts on testing methodologies.

Using Controllers and Routing in Web Forms

Developers don't need to switch to ASP.NET MVC to take advantage of web API controller architecture in their application. Adding Web API controllers to Web Forms allows the application to leverage simple, but powerful JSON based REST endpoints. Web API endpoints provide a low friction way of supplying data for consumption by AJAX calls from the client side of the application. In addition, these endpoints allow Web Forms to share data with external applications such as reporting services, mobile applications and more.

Web API controllers can be added to a Web Forms application and any point in the application's development cycle. New projects can include Web API as part of the File > New > Project template experience, while existing applications only need to add a WebApiConfig.

By default, Web API will be configured to emit XML from controller actions. To enable JSON formatting simply remove the XmlFormatter from configuration.

   // WebApiConfig.cs
   // Remove the XML formatter
   config.Formatters.Remove(config.Formatters.XmlFormatter);

To add Web API to existing applications, simply add configuration class WebApiConfig to enable Web API routing. The configuration below allows enables both attribute based routing and convention based routing.

//WebApiConfig.cs
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        // Web API routes
        config.MapHttpAttributeRoutes();
 
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional });
 
        // Remove the XML formatter
        config.Formatters.Remove(config.Formatters.XmlFormatter);
    }
}
 
//Global.asax.cs
public class Global : HttpApplication
{
 
    void Application_Start(object sender, EventArgs e)
    {
        ...
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

Testing Methodologies

How you test and what you test is less important than the fact that you test something. It’s likely the case that you’ll want to test each module or unit of code by writing unit tests. When all of the units of code unite to form a running application, you’ll want to do functional end-to-end testing. Below I detail the tools required:

Frameworks

Docs

Microsoft.VisualStudio.TestTools.UnitTesting

Microsoft’s standard unit testing library

XUnit

A free, open-source, community-focused unit testing tool for the .NET Framework

NUnit

One of the most popular unit testing frameworks for .NET


To take testing to the next level, consider complete a test automation solution for integration testing at the GUI level. One such way to integration test your GUI is with Progress Test Studio.

For a more detailed view of key considerations – software management tools, development process and methodology, tooling and development, backend methodologies, data and more – make sure to download Ed’s “Planning a Modern ASP.NET Web Forms Application” whitepaper.

Download Ed's Whitepaper

And while you’re at it, if you haven’t played with Telerik UI for ASP.NET AJAX consider downloading a free trial. This feature-complete UI toolkit includes more than 100 controls, themes and more and enables you to quickly and easily deliver a sleek UI and delightful UX, increasing your productivity and your app’s performance.


Sara Faatz
About the Author

Sara Faatz

Sara Faatz leads the Telerik and Kendo UI developer relations team at Progress. She has spent the majority of her career in the developer space building community, producing events, creating marketing programs, and more. When she's not working, she likes diving with sharks, running, and watching hockey.

Related Posts

Comments

Comments are disabled in preview mode.