Why use ASP.NET MVC instead of Web Forms?

I have been asked this question so many times.  Why use ASP.NET MVC instead of Web Forms?

My answer is simple, because it’s better. If you are not convinced please see below,

ASP.NET MVC is a completely new framework for building ASP.NET applications, designed from the ground up with SoC and testability in mind.

http://msdn.microsoft.com/en-us/magazine/dd942833.aspx

There are various positive points to moving towards MVC

1. TDD support out of the box as most of the design is based on interfaces.
2. SEO friendly URL by design (though now this is possible in ASP.NET 4 as well)
3. No ViewState (this may seem a bit of moving backward to some), but overall a good design decision.
4. Clean View Markup (no additional HTML emitted)
5. 100% extensible. You can add your own controller with IOC, switch view engines at will, control model binding at wish etc.
6. Rich UI support (possible through client side JS libraries like jQuery UI and others). Telerik has released some controls for MVC which includes Grid control as well (which are merely HTMLHelpers)
7. Session, JS, Ajax works. Validation is even more powerful with DataAnnotations and jquery.
8. Is MVC faster? Yes by default because of lack of viewstate and clean markup. But performance is subject and MVC by design is more performant that traditional ASP.NET webforms (though webforms can be made as fast as required.
9. Out of the box support for mitigating antiforgery attacks and XSS vulnerability (though asp.net does has this to some extent)
10. Out of the box minimal IOC support.
11. Full control over rendered HTML
12. Pluggable architecture

http://www.dofactory.com/topic/1336/why-should-we-use-mvc.aspx

Control over HTML enables developers to build Ajax applications more comfortably, and it facilitates adding more interactivity and responsiveness to existing apps. Direct control over HTML also means better accessibility for implementing compliance with evolving Web standards. The world of Web continually progresses, and ASP.NET MVC is closer than Web Forms to all emerging technology trends.

In addition, ASP.NET MVC uses interface-based contracts, which allow components to be more easily tested in isolation. As a result, cleaner and more testable code is often promoted as a good reason to embrace ASP.NET MVC. Frankly, I don’t consider this a valid primary reason for its selection. Writing clean code should be a practice that transcends the technology. While it can’t be denied that ASP.NET MVC is an inherently more testable framework, testability is more about isolating critical portions of code to gain visibility over internal state and control over input — two factors critical to attain the needed modularity. This practice has more to do with clean design and thoughtful code than with the surrounding framework. Put another way, your chances of writing bad code are nearly the same in Web Forms and ASP.NET MVC. But if you write bad code, MVC will make it easier to diagnose.

http://www.drdobbs.com/windows/231002811

An experienced developer will find MVC intuitive and easier to develop but a developer who has mainly worked in building websites and does not have software engineering skills might find MVC daunting and confusing especially after using regular asp.net for a while.

http://techkn0w.wordpress.com/2008/05/15/mvc-vs-regular-aspnet

Many of VB 6.0 developers had moved to ASP.net web development without knowing the basics of HTTP and web. For simulating windows form model development experience, webforms introduced event-driven approach and also introduced Viewstate and Postback.

http://weblogs.asp.net/shijuvarghese/archive/2008/07/09/asp-net-mvc-vs-asp-net-web-form.aspx

@ericphan from SSW has put a nice presentation on http://r.ssw.com/mvc-whats-all-the-fuss

How to do ASP.NET MVC Pagination in 30 minutes?

I needed web content pagination in my ASP.NET MVC 3 project. I found few solutions on the web, but end up accessing the first two below.

Telerik
Telerik has ASP.NET MVC extensions which I think is free and cool. To get the Telerik paging working in my MVC application I install Telerik MVC Extension nuget using Visual Studio 2010 NuGet Package Manager. Then I configured the controller, view and off you go. I got something like this.

Telerik Paging Grid

Telerik Paging Grid

MvcPaging
MVCPaging is also another easy way to achieve paging. Install MvcPaging nugget using Visual Studio 2010 NuGet Package Manager. Download the source code for MvdPaging form github and see the sample in MvcPaging.Demo. I also found this article useful to understand MvcPaging.

MvcPaging

MvcPaging

MvcContrib
I did not end up accessing MvcContrib, because MvcPaging fit my requirements. Here is a nice article with source code if you are interested.

I end up using MvcPaging for my application as I needed to be able to display boxes of information and paging on the bottom of the webpage as shown in the MvcPaging image above. But if you have lot of data and need to show it in a grid Telerik is a great option too.

Please let me know if you find any other better solutions for paging.

Keep you MVC Razor view DRY!

Always keep your view DRY (do not repeat). Suppose your website has any of the following,

  • Top menu
  • Left menu
  • Right menu

Or you have some HTML which is repeated in different view, then you could use the following,

@Html.Partial(“ViewFile”)
Renders the “ViewFile” view to an MvcHtmlString. It follows the standard rules for view lookup (i.e. check current directory, then check the Shared directory).

@Html.RenderPartial(“ViewFile”)
Does the same as Html.Partial(), except that it writes its output directly to the response stream. This is more efficient, because the view content is not buffered in memory. However, because the method does not return any output, @Html.RenderPartial(“MyView”) won’t work. You have to wrap the call in a code block instead: @{Html.RenderPartial(“ViewFile”);}.

@RenderPage(“ViewFile.cshtml”)
Renders the specified view (identified by path and file name rather than by view name) directly to the response stream, like Html.RenderPartial(). However, it seems to always use the current view’s model as the model for “ViewFile.cshtml”.

I prefer using @RenderPage(“ViewFile.cshtml”) only because the syntax is easier and it is more readable. If you are using MVC Area RenderPage by default will look into the View and Shared folder of the Area. If you want to point it to the common View folder in the root, then you have to mention the path. See example below.

@RenderPage(“~/Views/Shared/ViewFile.cshtml”)

References,

Introducing “Razor” – a new view engine for ASP.NET

ASP.NET MVC 3: Layouts and Sections with Razor