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

How to get rid of Event Viewer errors?

Error:
The description for Event ID ( 0 ) in Source ( Learning Management System ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event:

In registry:
1. Open regedit and go to HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Eventlog\Application\Learning Management System.
2. If ‘EventMessageFile’ is missing then the error above show is the event log. See the image below.

RegistryKey

RegistryKey

Code and solution :
The code below has ‘EventLog.CreateEventSource’ which creates ‘Learning Management System’ key under ‘HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Eventlog\Application’ in registy and also create ‘EventMessageFile’ expandable string value with details below:
Value name:EventMessageFile
Value data: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\EventLogMessages.dll

If the ‘EventMessageFile’ string is missing it show the error message above. On my xp machine I do not see the above message because when I run the code it creates the ‘Learning Management System’ and EventMessageFile string. If you have manually created the key in the registry on the Windows 2003 server because you are getting security exception while the application trying to create the key, then you have to mannually create the EventMessageFile string with the above mention details.

//Check if the ATLAS web services working
 if (reqHTTPGetADFWebServices.StatusCode == 0)
 {
    //Check if the event log application exist
    if (System.Diagnostics.EventLog.Exists("Application"))
    {
       //Check if the event log source exist
       if (!System.Diagnostics.EventLog.SourceExists("Learning Management System"))
       {
          //Register the source                            
          EventLog.CreateEventSource("Learning Management System""Application");
          // The source is created.  Exit the application to allow it to be registered.                          
       }
 
   // Write an source, message and type entry to the event log.  
   System.Diagnostics.EventLog.WriteEntry("Learning Management System""FAILED:
 ADF web services are not available."EventLogEntryType.Error);                      
}

Resources:
Error Message When ASP.NET Application Tries to Write New EventSource in the EventLog

How to check whether the application is single sign on or form authentication from C# code?

Suppose you have a C# web application which need to run on a internal (inside network) and external (DMZ) web servers. These two application talks to the same database. The internal application need single sign on (SSO) and the external application is form authentication. Now in Visual studio you have one project for both these applications. Depending on the web.config’s configurations the application either do SSO or form authentication.

For SSO the web.config settings are,
<authentication mode="Windows"></authentication>

For form the web.config settings are,
<authentication mode="Forms">
<forms name="LegalAidLogin" loginUrl="~/login.aspx" defaultUrl="~/default.aspx" timeout="30" protection="All" enableCrossAppRedirects="true">
</forms>
</authentication>

Now, when you in login page which you have to need to check the authentication mode on page load, use the code below,

// Load web.config
System.Xml.XmlDocument config = new System.Xml.XmlDocument();
config.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
System.Xml.XmlNode node = config.SelectSingleNode(”//configuration/system.web/authentication”);

// Check the login mode for forgot your password
if (node.Attributes["mode"].Value == “Forms”)
{}

If you are already login in and you need to check the authentication mode use code below,

if (HttpContext.Current.User.Identity.AuthenticationType != “Forms”)
{}