Wednesday, March 24, 2010

.NET 4 ASP.NET MVC 2 70-515 Catto Code Cracking Post #7

.NET 4 ASP.NET MVC 2 70-515 Exam Prep Catto Code Crackin Post #7

Developing a Web Application by Using ASP.NET MVC 2 (13%)

In this 7th post of the Catto Code Crackin series we’ll continue with the section ‘Developing a Web Application by using ASP.NET MVC 2 . In the .NET 3.5 exam there wasn’t a MVC sections. In the .NET 4 exam the section iis listed as 13%.

Microsoft announced the .NET 4.0 Beta Exams on St. Patrick’s Day which are a free exam & if passed you get certified. Much of the study & prep materials are not available yet, therefore I’ve been studying for a similar exam 70-562 which is the .NET 3.5 ASP.NET Application Development. I hope by posting this content it will help myself along with other people in the community learn & get excited about .NET 4. If you are serious about studying for a MS Exam 2 must have resources: the Self Paced training books are a great books to buy along with practice exams from eBay.

Let’s Start with the Official Skills Measured:

Developing a Web Application by Using ASP.NET MVC 2 (13%)

ASP.NET MVC 2 is an additional framework that is installed on top of .NET 3.5 SP1. Once it’s installed in visual studio we can create a new project of type MVC.

Lets look @ the frameworks installed:

In the book there is no MVC. Let’s go get some content that are important points

Lets first go to dub dub dub dot a s p . net / mvc www.asp.net/mvc. This is the best place that I enjoy to goto first. I liked to hear MVC is similar to classic asp since I’ve spent quite a bit of time with classic asp 3.0. There are no server controls only html helpers so therefore there isn’t as much hidden & more flexibility, There are some fundamentals too. Da Gu’s MVC Posts!

Let’s be clear the skills measured is MVC2. There are some great items we can learn about the enhancements of the second Let’s hit this after an overview & file structure example.

MVC – Model View Controler http://www.asp.net/mvc/whatisaspmvc/

There is a great forum question on SO

The best answer from Mr. Flowers

MVC borrows good thinigs from Rails, AJAX hiding js could hurt us, JQuery has taken over the world it’s Open Source.

Here are other content public from some skills people. Stephen Walther has a stellar blog with many tips on MVC he also does many of the vids on the official ms site. Jeffery Palermo has some good MVC content too Here is a great post by Hanselminutes.

PodCasts!\Some great pods in order of quality with best quality @ top in my opinion:

DNR ASP.NET MVC 2 - Show 533 w/ Phil Haack

Hanselminutes ASP.NET MVC 2 - #206 w/ Phil Haack

Deep Fried Bytes ASP.NET MVC ‘in action’ – #48 Jeffery Palermo, Ben Scheirman, & Jimmy Bogard

Polymorphic Pod ASP.NET MVC Jeffery Palermo

Code Example - Nerd Dinner on CodePlex is a great code example: http://nerddinner.codeplex.com/

MVC file structure differs from web forms, Let’s take a look at some:

Model - Core info Classes. The model folder tree could be an example here:

Models
Event.cs
EventRepository.cs
IEventRepository.cs
GeekEvent.dbml
GeekEvent.dbml.layout”
GeekEvent.dbml.cs
RuleViolation.cs

Views - presentation HTML Markup. Views are a folder here is an example of a folder tree of the views folder:
Views
Account
LogOn.aspx
Resiger.aspx
Event
Create.aspx
Details.asp
Home
About..aspx

Controller control flow logic. Interacts with model & view. An example of the Controller folder explorer tree would be:

Controller
AccountController.cs
EventController.cs
HomeConttroller.cs
SearchController.cs

Controllers == app flow control logic

View == html page, scripts

Model === application logic such as all business logic, validation, data access layer

The first skilled measured is Create Custom Routes:

Let’s look @ this library page on ASP.NET Routing

A route is a URL pattern that is mapped to a handler. 
{controller}.mvc/{action}/{id}

Adding constraints to routes is first on the list of skills measured here is a nice link to the library. It states: we can specify that values in the parameters meet certain constraints. We can add constants to ensure that the URL parameters contain values that will work in your app.

X 7 2 Create controllers & actions

Create controller by right click on the controller folder, select add controller. Ensure that the name of the controller ends in controller example EventController or Default1Controller

Adding Actions to a controller You add actions by adding a new method to the controller. Here are some requirements of actions:


· The method must be public.

· The method cannot be a static method.

· The method cannot be an extension method.

· The method cannot be a constructor, getter, or setter.

· The method cannot have open generic types.

· The method is not a method of the controller base class.
· The method cannot contain ref or out parameters.

Action filters There are many types of action filters in MVC such as
Authorization Filters – security decision whether to execute action filter

Action Filter – wraps action method execution. This filter can perform additional processing, such as providing extra data to the action method, inspecting the return value, or canceling execution of the action method.

Result Filter & Exception Filter.

There is a project on codeplex ASP.NET MVC Action Filters this projects states it has the following planned Action Filters
· Action filter for creating easy REST API with JSON and XML
· Action filter for logging scenarios
· Action filter for microsoft health monitoring
· Action filter for client caching
· Action filter for client compression
· Action filter for server caching
· Action filter for controller execution time measurement

It builds on the nerd Dinner example. There is a folder in the directory Mvc.ActionFilters with some files in it such as AutoRefreshAttribute.cs, ClientSideCashe.cs. It may be worth taking a look at too.

X 7 3 4 Structure of ASP.NET MVC app

Content Files & Folders
URLs map to files such as an .aspx file
ASP.NET MVC maps differently from ASP.NET web forms. Web forms mapst to pages or handlers, MVC maps URL’s to controller classes such as AccountController.cs

ASP.NET MVC 2

Great resource is the What inside MVC a pdf
New Features of MVC 2 are: MVC2 buils on MVC 1.0 & enhances features therefore it’s compatible with ASP.NET MVC 1.0 all skills continue to apply.
MVC 2 provides us Strongly Types Helpers

helpers (ex. MVC 1.0 <% html.TextBox(“ProductName”, Model.ProductName) %>

MVC 2 <% Htlm.TextBoxFor(model => model.ProductName) %>


This gives us intellisese to since strongly typed which is good.

Areas

MVC 2 provides us more support. Examples for support:

Asynchronous Contorllers - support that enables long running tasks in parallel

Binding Binary Data with Model Binders

DataAnnotations Attriubues

DefaultValueAttribute in Action Method params

MVC 2 - Client Side Validation
MVC 2 - VS 2010 code snippets

MVC2 has some cashing features right? No MVC2 doesn’t really have any new cashing features.

Some more content about ASP.NET MVC:
Download Rrameowrks:
ASP.NET MVC 1.0

ASP.NET MVC RC 2

ASP.NET MVC Forum

MVC 3

Road map for mvc 3 productivity, AJAX, architecture & performance

CAPTCHA
AJAX Helpers
MEF for actitecutre & App Scaffolding
Performance Improved Cashing Support

--------------------------------------------------------

Code:

Here are some good code examples http://code.msdn.microsoft.com/aspnetmvcsamples s

In the web.config let’s check out this code from nerddinner: Let’s display it both collapse just a two sections of the system.web & expanded:

<system.web>

<httpHandlers> … </httpHandlers>

<httpModules>…<httpModules>

</system.web>

<httpHandlers>

<remove verb="*" path="*.asmx" />

<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" />

<add verb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

</httpHandlers>

<httpModules>

<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

</httpModules>

</system.web>

Let’s look a a drastically different default.aspx page than we ar eused to

Here is the default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="NerdDinner._Default" %>

<%-- Please do not delete this file. It is used to ensure that ASP.NET MVC is activated by IIS when a user makes a "/" request to the server. --%>

& default.cs

using System.Web;
using System.Web.Mvc;
using System.Web.UI;
namespace NerdDinner {

public
partial class _Default : Page {

public void Page_Load(object sender, System.EventArgs e) {

HttpContext.Current.RewritePath(Request.ApplicationPath, false);

IHttpHandler httpHandler = new MvcHttpHandler();

httpHandler.ProcessRequest(HttpContext.Current);

}


}

}

X 7 3 4 Content Files & Folders
Let’s check out the msdn library & here is
Global URL Routing Defaults:



Routes are initialized in the Application_Start method of the Global.asax file. The following example shows a typical Global.asax file that includes default routing logic.

public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
7 4 5 How bout ViewMasterPage (TModel Class ) Crackin code ViewMasterPage is an item on the official skills measured.
.NET Framework Class Library
ViewMasterPage<(Of <(TModel>)>) Class
Represents the information that is required in order to build a strongly typed master view page
Assembly: System.Web.Mvc (in System.Web.Mvc.dll)
Public Class ViewMasterPage(Of TModel) _
Inherits ViewMasterPage
public class ViewMasterPage<TModel> : ViewMasterPage

7 4 6 This is the last skill measured FYI

ViewUserControl(TModel) Class

Public Class ViewUserControl(Of TModel) _
    Inherits ViewUserControl
public class ViewUserControl<TModel> : ViewUserControl

Represents the information that is required in order to build a strongly typed user control.

Namespace: System.Web.Mvc
Assembly: System.Web.Mvc (in System.Web.Mvc.dll)

7 4 1 b http://msdn.microsoft.com/en-us/library/dd410596%28VS.100%29.aspx

Begin Form Helper example course theres more like listbox, dropdownlist, textbox ect.

<% using(Html.BeginForm("HandleForm", "Home")) %>
<% { %>
    <!-- Form content goes here -->
<% } %>
<% Html.BeginForm(); %>
    <!-- Form content goes here -->
<% Html.EndForm(); %>
Debugging Code 
       Example such as NerdDinner my first MVC Error is a classic familiar ASP.NET yellow screen of death. Debugging is so fun. Here is an example of a MVC Parser Error nice & familiar.

Server Error in '/' Application.

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.


Parser Error Message: Could not load type 'NerdDinner.MvcApplication'.
Source Error:

Source File: /global.asax Line: 1

Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3082
Automapper is a third party tool I don’t know about would this be part of MVC 3

We now have covered over much of ASP.NET MVC 2 & some of the skills that will be covered on the exam. Let’s review more code & watch some more videos & do it all over again to get a better understanding, maybe create some sample exam questions. What is the future?

Also this is very interesting & similar: crackin code is fun

MVVM – Model View View Model (not on exam)

---------------------------------------------------------------------------

That’s all there will be more,

Catto

No comments: