Nowadays, most of us are choosing for ASP.NET MVC for their new web applications. However, this may not be always possible and we might be required to use Web Forms for our new projects. In these kind of cases few of us have this confusion – If I develop my project using Web Forms and later want to migrate to MVC, how complicated this task would be?. On this question, developer will answer less. However, if we can keep in mind certain guidelines before starting development on a Web Forms project today, at later stage switching to MVC would be less painful than otherwise. Here I am listing my top 10 recommendations in dealing with such a situation:-
- Use Class libraries wherever possible
If we observe the evolution of .NET framework over the years we will realize that Class Libraries are best for writing code that is independent if any particular type of UI. If we isolate as much code as possible in class libraries then reusing then during and after the migration would be quite simple.
- Try to use jQuery Ajax over Web Forms specific techniques
Web Forms supports AJAX Extensions to help us develop Ajax enabled applications. However, these controls are specific to Web Forms. Instead of relying on them we may consider using standard Ajax techniques such as jQuery $ .ajax() or XMLHttpRequest object. Using this way,our Ajax code will be quite portable to any other web development framework including MVC.
- Should make habitual use of Web API
Web API was introduced along with MVC but now it’s the part of the ASP.NET framework and we can use Web API in Web Forms applications too. Because Web API controllers will be 100% reusable in MVC applications.
- Always skip server control level UI properties
ASP.NET Web Forms make UI development quite easy. Just drag-n-drop controls, set their properties and your UI are ready. One common mistake most of the developer make is to set UI related properties of the server controls. E.g. Back Color, Forecolor, Font-Name and so on. These properties are converted into style attribute of the respective control tag. A recommended approach is to place all such styling information in CSS style sheets and then use Class Name property of the server controls to attach a CSS class.
- Skip Web Forms specific features
Web Forms use several features that are specific to Web Forms. These features are not available in MVC. Avoid such features for new projects because it will be difficult to migrate them to MVC projects.
- Create forms without ViewState as much as possible
One big reservation against Web Forms is ViewState. Many Web Forms developers design their data entry pages in such a way that multiple tasks (Insert, Update and Delete) happen on a single page. This may require page ViewState enabled resulting in bulky forms. We should see if these tasks can be separated into their own pages in an attempt to avoid ViewState altogether. For example, say we have a GridView at the top of a page. Selecting a row in the grid displays its content for editing at the bottom half of the page. That means selecting and editing are happing in one page itself and ViewState might be necessary. Instead, think if the grid can have hyperlinks (instead of Select postback buttons) that points to another web form where data editing takes place. In this case we can turn the ViewState off for the page housing the grid since there is no postback and editing is happening on a different page.
- Try to create reusable UI through User Controls
Developers have two choices for creating reusable user interfaces – Web User Controls and Custom Server Controls. If we wrap our reusable UI as a Web User Controls our development will be easy. This is because we have raw HTML and code and MVC does offer a close equivalent in the form of partial pages.
- Create standard HTML over server controls wherever possible
Rich server controls is one of the biggest strength of Web Forms applications. Controls such as GridView and ListView are quite popular. However, using these controls make our migration complicated because there is no direct equivalent in MVC. Although we may not avoid using them altogether try to minimize their use whenever possible. For example, if you don’t need to access Label controls programmatically, don’t use them. Use standard <span> or <lable> tags. This way we can reuse that much markup readily in MVC.
- Understand in terms of MVC even for Web Forms applications
Although Web Forms are not designed keeping MVC design pattern in mind, nothing stops us from thinking in MVC terms. The separation of concern as enforced by MVC can be done in Web Forms also. When we develop the Web Form try thinking in terms of its Model, Controller Actions and View.
- Learn and use Design Patterns and SOLID principles
Most of us are accustomed to drag-n-drop development. Our applications are UI centric. Although these applications may meet the requirements at a given point of time, they become difficult to maintain and extend. This happens because a lot of code is stuffed directly into Web Forms.