Tuesday, February 7, 2012

Asp .Net MVC 3 Getting Started

Asp .Net MVC is a complete alternative to Asp .Net Web Forms. Asp .Net emphasizes testability, doesn't conceal how the web works and has excellent control over HTML.

Asp .Net MVC is based on MVC pattern which dates back to 1978 and the Smalltalk project at XEROX PARC. Asp .Net MVC is built as a  series of independent replaceable components and the developers can choose  the routing system, the view engine, the controller factory  or ORM. 

Asp .Net MVC 3 is built on .Net 4. It comes with a new view engine called Razor and tighter integration with jQuery.

Still Asp .Net Web Forms is the superior technology when it comes to intranet applications. ASP .Net 4 has become more web standard compliant and you can use Razor view engine. Just like Asp .Net MVC, Asp .Net 4 also support excellent URL routing features.

Let's start creating an Asp .Net MVC 3 project. The blog Asp .Net MVC 2 with Visual Studio 2010 or Visual Web Developer 2010 I had covered the basics of getting started with MVC 2.

Asp .Net MVC New Project Shalvin
As already mentioned Asp .Net MVC 3 is coming with the Razor View Engine, thought you can use the Web Forms View Engine with Asp .Net MVC that is not a recommended practice. Microsoft has made it clear that the future developments will be based on Razor view engine.

So I am selecting Razor as the View engine.

If you navigate to Scripts folder in the Solution Explorer of Asp .Net MVC 3 project you can lot more jQuery files that the previous version.

Controllers and Action Methods
In MVC architecture, incoming requests are handled by controllers. In ASP.NET MVC, controllers are just simple C# classes .
Each public method in a controller is known as an action method.  You
can invoke an Action Method from the Web via some URL to perform an action.
The MVC convention is to put controllers in a folder called Controllers, which Visual Studio created for us when it set up the project.


The controller’s job to construct some data, and it’s the view’s job to render it as HTML. The
data is passed from the controller to the view.
One way to pass data from the controller to the view is by using the ViewBag object. This is a member
of the Controller base class. ViewBag is a dynamic object to which you can assign arbitrary properties,
making those values available in whatever view is subsequently rendered.



I have already covered creating controllers in the blog Asp .Net MVC 2 with Visual Studio 2010 or Visual Web Developer 2010 which remains the same in Asp .Net MVC 3.

When we return a ViewResult object from an action method, we are instructing MVC to render a view. We create the ViewResult by calling the View method with no parameters. This tells MVC to render the default view for the action.


We can return other results from action methods besides strings and ViewResult objects. For instance a RedirectResult can be used to  redirected browser  to another URL. If we return an HttpUnauthorizedResult, we force the user to log in. These objects are collectively known as action results, and they are all derived from the ActionResult class.


The Razor view is quite different from that of Web Forms View Engine. Refer my blog Razor View Engine and Visual Studio for more information on Razor View Engine.


@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>






using System.Web.Mvc;

namespace MvcApplicationShalvin.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
          ViewBag.Name = "Shalvin P D";
          ViewBag.Site = "http://shalvin.net";
         
            return View();
        }

    }
}





@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@ViewBag.Name

<br />
@ViewBag.Site


3 comments: