<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Fredrik Normén : MVC Framework</title><link>http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx</link><description>Tags: MVC Framework</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>What role does the User Control has in the ASP.Net MVC?</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/11/28/what-role-does-the-user-control-has-in-the-asp-net-mvc.aspx</link><pubDate>Fri, 28 Nov 2008 08:28:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6757988</guid><dc:creator>Fredrik N</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6757988</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/11/28/what-role-does-the-user-control-has-in-the-asp-net-mvc.aspx#comments</comments><description>&lt;P&gt;Several developers have asked me about the use of User Controls within the ASP.Net MVC, so I decided to write down what I think about it.&lt;/P&gt;
&lt;P&gt;A User Control is a piece of the User Interface which is sometimes created to be reused on several of Web Forms, some are created as a result from a Refactoring of the User Interface code. A User Control has a code-behind and the User Control is living its own life mostly if the Web Form and User Control is well defined. Some people tend to access the Web Form from a User Control and I think that is bad practice, it will make the User Control dependent to a specific Web Form, so a better solution is to use events which a Web Form can subscribe to, if we need to pass data from the Web Form to the User Control, we can do it by adding properties to the User Control. Now how about a User Control in the ASP.Net MVC?&lt;/P&gt;
&lt;P&gt;As you all may know there is a View and the View has a Controller, the Controller work like a contract between the View and the Model. The Model is where we have our Business Logic and domain entities.&lt;/P&gt;
&lt;P&gt;NOTE! I have seen several developers using the Controllers as a replacement of Business object, that is not the role of a Controller, the Business logic should be in the Model.&lt;/P&gt;
&lt;P&gt;I will assume that a View has only one Controller and should only be dependent on one, so in the ASP.Net MVC the User Controls will take a "new" kind of role. A User Control in the ASP.Net MVC is part of the View and should reuse the same Controller as the View. If not, our View will have dependency to several different Controllers and we will need to take this into consideration when writing our test. We should try to avoid as many dependency as possible, too many dependency will make the code harder to read and maintain etc. So what role does the User Control take in a View? They will be a result of Refactoring of the User Interface code, and will more or less be like a simple "include" file used by the VIew (Like a extracted method in a Class). A User Control and a View together will create a complete View and this complete View should only work against one Controller. Some developers tend to create and use a User Control after the same pattern they are used to when they work with Web Forms. Instead of using a code-behind, they bind the User Control to it's own Controller. By doing so, a View which will use the User Control, will have a indirect dependency to several Controllers. Instead of making sure a View has its own Controller, it now has several, if we have several User Controls on the same View and all User Controls uses their own Controllers, we will sooner or later create a Death Star. If we should use a User Control in the ASP.Net MVC it's the Controller of the View which will return the Model which the User Control and View will use, right?! So it's the same Controller that should handle the User Controls actions. If we want to create a User Control and see it as a self lived unit,&amp;nbsp;I think it should be important to let its own Controller give the Model to the User Control, but how it's designed today, we can't. It's the Controller used by the View which must also get the Model for the User Control.&lt;/P&gt;
&lt;P&gt;How about reusing a User Control on different Views if they should more or less serve as a "include" file and use the same Controller as the View? For me it's all fine, because the User Control will share the same Controller as the View, and the User Control should try to avoid having its own form element. If we want to reuse a User Control on several of Views, where the User Control has its own Controller, the Controller of the View will have a indirect dependency to the User Control and suddenly the Controller of a View will have several reason to change (If the User Control need to be updated and that may lead to an update of the Controller.). To solve this, we shouldn't try to reuse a User Control as long as it can't server as a simple include of HTML element, where the User Control will not have a form element pointed to a separate Controller served only by the User Control. &lt;/P&gt;
&lt;P&gt;I hope some of you can try to convince me if my assumptions is totally wrong, or at least you can try ;)&lt;/P&gt;
&lt;DIV class=posttagsblock&gt;&lt;A href="http://technorati.com/tag/MVC" rel=tag mce_href="http://technorati.com/tag/MVC"&gt;MVC&lt;/A&gt;, &lt;A href="http://technorati.com/tag/ASP.NET" rel=tag mce_href="http://technorati.com/tag/ASP.NET"&gt;ASP.NET&lt;/A&gt;&lt;/DIV&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6757988" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>ASP.Net MVC Framework - How do I design my apps with the MVC pattern and +P</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/04/22/how-do-i-design-my-apps-with-mvc-p-what-do-i-refer-to-when-i-say-mvc-p.aspx</link><pubDate>Tue, 22 Apr 2008 20:36:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6121915</guid><dc:creator>Fredrik N</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6121915</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/04/22/how-do-i-design-my-apps-with-mvc-p-what-do-i-refer-to-when-i-say-mvc-p.aspx#comments</comments><description>&lt;P&gt;When I'm playing around with the ASP.NET MVC Framework I have created several prototype applications with different solutions to solve some "problems." In this post I will write down how I combine a Presentation Model (When I talk about Presentation Model in this post, I refer to a model which purpose is only to define a model for presentation purpose) with the MVC, it's what the P in MVC+P stands for.&amp;nbsp; &lt;BR&gt;&lt;BR&gt;When I build apps today I use Domain Driven Design and I use the MVC pattern in some of my web based applications. As many of you know the M in MVC stands for the Model and contains our entities, business logic can data access components. Often when I build my apps with the ASP.Net MVC Framework (still only use in as a prototype because the framework is under development) I create my own custom Controller Factory to support Dependency Injection (DI). For example I use constructor injection or setter injection to inject my Repositories or manual injection of Mock objects during test. I use Spring.Net as my DI framework because it’s the framework I like the most.&lt;/P&gt;
&lt;P&gt;Here is a simple example how my Controllers can look like and are prepared for constructor injection:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;HomeController &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;Controller
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: #2b91af"&gt;ICompanyRepository &lt;/SPAN&gt;_companyRepository;

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;HomeController() : &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;CompanyRepository()) { }

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;HomeController(&lt;SPAN style="COLOR: #2b91af"&gt;ICompanyRepository &lt;/SPAN&gt;companyRepository)
    {
        &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._companyRepository = companyRepository;
    }

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ActionResult &lt;/SPAN&gt;Index()
    {
        CompanyInfo companyInfo = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._companyRepository.GetCompanyInfo();
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;RenderView(&lt;SPAN style="COLOR: #a31515"&gt;"Index"&lt;/SPAN&gt;, companyInfo);
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;As you can see I create an instance of a CompanyRepository in the default constructor, the reason to this is because if I create a normal instance of the HomeController, I want it to use a default Repository. I also have a constructor which takes a ICompanyRepository as an argument, this is because to enable constructor injection. I can use the constructor to pass in my Mock object of the CompanyRepository, or I can use Spring.Net to inject a ICompanyRepository, which I use in my own custom Controller Factory. Here is how my test could look like where I have a Mock object for the CompanyRepository: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;    &lt;SPAN style="COLOR: #2b91af"&gt;MockCompanyRepository &lt;/SPAN&gt;mockCompanyRepository = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MockCompanyRepository&lt;/SPAN&gt;();

    mockCompanyRepository.Create(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;CompanyInfo(&lt;SPAN style="COLOR: #a31515"&gt;"........"&lt;/SPAN&gt;));

    &lt;SPAN style="COLOR: #2b91af"&gt;CompanyController &lt;/SPAN&gt;companyController = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CompanyController&lt;/SPAN&gt;(mockCompanyRepository);

    var result = &lt;SPAN style="COLOR: #2b91af"&gt;CompanyController&lt;/SPAN&gt;.Index() &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;RenderViewResult&lt;/SPAN&gt;;

    &lt;SPAN style="COLOR: green"&gt;//Assert...
&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;&lt;EM&gt;Note: If you have read my Step by Step Guide, I use the LINQ to SQL's DataContext object directly in my Controllers, it's because they can sometimes serve as a "Repository" because the power of the LINQ.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;When I use MasterPage which should always render a part of my Model, like content for a Menu etc, or when I need to render more than one entities of my Model, I need to make sure my Controllers will fill the ViewData property with more information than just one single entity from my Model. In this case I may have several Repositories which I need to call from my Controllers. If I use constructor injection, I need to add an argument to a Controller's constructor for each Repository. This can make the constructor cumbersome. To solve the issue and reduce the number of argument, I instead use setter injection, so I inject the Repositories needed through a set method (using properties). By doing this I can easy manually inject my Mock objects in my test:&lt;/P&gt;&lt;PRE class=code&gt;    &lt;SPAN style="COLOR: #2b91af"&gt;MockCompanyRepository &lt;/SPAN&gt;mockCompanyRepository = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MockCompanyRepository&lt;/SPAN&gt;();
    &lt;SPAN style="COLOR: #2b91af"&gt;MockCustomerRepository &lt;/SPAN&gt;mockCustomerRepository = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MockCustomerRepository&lt;/SPAN&gt;();

    &lt;SPAN style="COLOR: green"&gt;//...

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CompanyController &lt;/SPAN&gt;companyController = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CompanyController&lt;/SPAN&gt;();

    companyController.CompanyRepository = mockCompanyRepository;
    companyController.CustomerRepository = mockCustomerRepository;

    var result = &lt;SPAN style="COLOR: #2b91af"&gt;CompanyController&lt;/SPAN&gt;.Index() &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;RenderViewResult&lt;/SPAN&gt;;

    &lt;SPAN style="COLOR: green"&gt;//Assert...
&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;I can still reuse my own custom Controller Factory without changes to support setter injection, because I simple only let Spring.Net know that I want to inject my Repositories by using setter injection. When I use setter injection, I don't create an instance of the default Repository or Repositories in the default constructor of the Controller.&lt;/P&gt;
&lt;P&gt;The following is an example of a Controller which uses to Repositories and pass two entities to the "View":&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;HomeController &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;Controller
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: #2b91af"&gt;ICompanyRepository &lt;/SPAN&gt;_companyRepository;
    &lt;SPAN style="COLOR: #2b91af"&gt;ICustomerRepository &lt;/SPAN&gt;_customerRepository;

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;HomeController() { }


    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ActionResult &lt;/SPAN&gt;Index()
    {
        ViewData[&lt;SPAN style="COLOR: #a31515"&gt;"CompanyInfo"&lt;/SPAN&gt;] = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.CompanyRepository.GetCompanyInfo();&lt;BR&gt;        ViewData[&lt;SPAN style="COLOR: #a31515"&gt;"Customer"&lt;/SPAN&gt;] = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.CustomerRepository.GetCustomer();
        
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;RenderView();
    }

    
    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ICustomerRepository &lt;/SPAN&gt;CustomerRepository
    {
        &lt;SPAN style="COLOR: blue"&gt;get
        &lt;/SPAN&gt;{
            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._customerRepository == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
                &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._customerRepository = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CustomerRepository&lt;/SPAN&gt;();

            &lt;SPAN style="COLOR: blue"&gt;return this&lt;/SPAN&gt;._customerRepository;
        }
        &lt;SPAN style="COLOR: blue"&gt;set
        &lt;/SPAN&gt;{
            &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._customerRepository = &lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;;
        }
    }


    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ICompanyRepository &lt;/SPAN&gt;CompanyRepository
    {
        &lt;SPAN style="COLOR: blue"&gt;get
        &lt;/SPAN&gt;{
            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._companyRepository == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
                &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._companyRepository = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CompanyRepository&lt;/SPAN&gt;();

            &lt;SPAN style="COLOR: blue"&gt;return this&lt;/SPAN&gt;._companyRepository;
        }
        &lt;SPAN style="COLOR: blue"&gt;set
        &lt;/SPAN&gt;{
            &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._companyRepository = &lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;;
        }
    }

}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The Index method will now return two entities. Assume the CompanyInfo is for the MasterPage and the Customer for the Content Page. The ViewData property in this case will sort of be a Presentation Model (&lt;EM&gt;"The essence of a Presentation Model is of a fully self-contained class that represents all the data and behavior of the UI window"&lt;/EM&gt;) but don't contain any behavior.&lt;/P&gt;
&lt;P&gt;Something that can make the above Controller or other Controllers ugly and start to smell, is when most action method must set the ViewData for the Model which the MasterPage will render. There is a tiny little problem regarding to how I will work and think when it comes to Test Driven Developement, when I use a MasterPage or User Controls on a View which will render different models. The problem is, when I create a test for a Controller before the Controller is implemented, I must know about what Models the UI will render, all entity objects even for the MasterPage and User Controls.&amp;nbsp; I can't only assume that the CustomerController's List action method will return a list of Customers, I also need to have in mind the model the MasterPage and User Controls will use and when writing the test. Well basically I can ignore it, but it can affect how my ViewData will look like, and what key I need to use to get access to my list of Customers etc. This is something that I don't want to care about for every action method I want to create a test for. I also like the way I can type the ViewData property for the View. To solve the issue I decided to create a base class called PresentationModel. This class have one typed property for the model the MasterPage should render, MasterPageModel and one for the Repository which will get the Model for the MastePage, here is an example:&lt;/P&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;PRE&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;PresentationModel
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;private &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CompanyInfo &lt;/SPAN&gt;_companyInfo;

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CompanyInfo &lt;/SPAN&gt;MasterPageModel
    {
        &lt;SPAN style="COLOR: blue"&gt;get
        &lt;/SPAN&gt;{
            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.CompanyRepository == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
                &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ApplicationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"A ICompanyRepository must be set"&lt;/SPAN&gt;);

            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(this._companInfo == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)&lt;BR&gt;                &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._companyInfo = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.CompanyRepository.GetCompanyInfo();

            &lt;SPAN style="COLOR: blue"&gt;return this&lt;/SPAN&gt;._companyInfo;
        }
        &lt;SPAN style="COLOR: blue"&gt;set
        &lt;/SPAN&gt;{
            &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._companyInfo = &lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;;
        }
    }

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ICompanyRepository &lt;/SPAN&gt;CompanyRepository { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;If the Model or the MasterPage is changed and that affect what the MastePage should render, I only need to change what model the PresentationModel class returns through the MastePageModel property, I don't need to go through all action methods and update them. That is one benefit with this design solution.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Note: I use a property for the CompanyRepository to make it easy to add a Mock object in my tests.&lt;/EM&gt; &lt;BR&gt;&lt;BR&gt;The PresentationModel is the type I specify for the MastePage to make it typed. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public partial class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Site &lt;/SPAN&gt;: System.Web.Mvc.&lt;SPAN style="COLOR: #2b91af"&gt;ViewMasterPage&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;PresentationModel&lt;/SPAN&gt;&amp;gt;
{
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;I then create a sub class of the PresentationModel called GenericPresentationModel. The GenericPresentaionModel class is a generic class which inherits from the PresentationModel and have a generic property called Model. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;GenericPresentationModel&lt;/SPAN&gt;&amp;lt;T&amp;gt; : &lt;SPAN style="COLOR: #2b91af"&gt;PresentationModel
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;GenericPresentationModel() {}

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;GenericPresentationModel(T model)
    {
        &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Model = model;
    }

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;T Model { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;The Controllers can now use the GenericPresentationModel and pass it to the View, the following Controller has an Action method which will render a Customer:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ActionResult &lt;/SPAN&gt;Index()
{
    &lt;SPAN style="COLOR: #2b91af"&gt;Customer &lt;/SPAN&gt;customer = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.CustomerRepository.GetCustomer();

    &lt;SPAN style="COLOR: #2b91af"&gt;var &lt;/SPAN&gt;presentationModel = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;GenericPresentationModel&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;&amp;gt;(customer);

    &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;RenderView(&lt;SPAN style="COLOR: #a31515"&gt;"Index"&lt;/SPAN&gt;, presentationModel);
}&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;Because I pass the GenericPresentationModel to my View, I can also type the View:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public partial class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CustomerView &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;ViewPage&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;GenericresentationModel&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;&amp;gt;&amp;gt;
{
}&lt;/PRE&gt;
&lt;P&gt;Then my ViewData property of my View will be of Type GenericPresentationModel&amp;lt;Customer&amp;gt;. So I can easy get access to my Customer by using ViewData.Model. &lt;BR&gt;&lt;BR&gt;The following example shows how the test of an Action which will render a Customer could look like:&lt;/P&gt;&lt;PRE class=code&gt;&lt;BR&gt;        &lt;SPAN style="COLOR: #2b91af"&gt;MockCustomerRepository &lt;/SPAN&gt;mockCustomerRepository = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MockCustomerRepository&lt;/SPAN&gt;();

        &lt;SPAN style="COLOR: green"&gt;//...

        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;HomeController &lt;/SPAN&gt;controller = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;HomeController&lt;/SPAN&gt;();
        controller.CustomerRepository = mockCustomerRepository();

        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;result = controller.Index() &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;RenderViewResult&lt;/SPAN&gt;;

        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;presentationModel = result.ViewData &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;GenericPresentationModel&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;&amp;gt;;

        &lt;SPAN style="COLOR: green"&gt;//Assert.....
&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;Because I don't use the MastePageModel property in my test of the Index method, I don't need to inject the Mock Repository for the PresentationModel base class. When I created the PresentationModel I have already created a test to make sure the PresentationModel returns the correct data, so I don't need to care about testing that again in my other tests. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;I still use this approach in a prototype solution to see how well it will work. When I work with Domain Driven Design I often need to render different information from different entities from my domain model. In my case I don't always want to pass several entities to my Views, so instead I create new object that will combine information from different entities, with another word, I create a Presentation Model. Most larger apps which I have created with the ASP.Net MVC Framework, often result in the use of a Presentation Model, and that is the P in the MVC+P. I know that several people don't like this because it requires us to create a lot of extra classes, but the benefits is that the Views will use the Presentation Model and don't even need to know about the domain model. If we don't pass a single entity from our domain model to the View, instead we use a Presentation Model and map the domain model to the Presentation Model, we can change the domain model, and it doesn't need to change the Views, only the mapping between the Presentation Model and the domain model. To be honest I don't create a complete Presentation Model, I still pass entities from the domain model to the View, mostly to keep it simple and not spend extra time to write new classes for each View. &lt;BR&gt;&lt;BR&gt;I'm interested about what you think, how you solve similar problems, do you use similar solutions that I do and have notice problems that I haven't notice yet, please let me know.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6121915" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>ASP.Net MVC Framework pre- Preview 3 - A Step by Step guide to create a simple web app.</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/04/17/asp-net-mvc-framework-pre-preview-3-a-step-by-step-guide-to-create-a-simple-web-app.aspx</link><pubDate>Thu, 17 Apr 2008 10:00:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6105638</guid><dc:creator>Fredrik N</dc:creator><slash:comments>24</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6105638</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/04/17/asp-net-mvc-framework-pre-preview-3-a-step-by-step-guide-to-create-a-simple-web-app.aspx#comments</comments><description>&lt;P&gt;Yesterday I posted a &lt;A class="" href="http://weblogs.asp.net/fredriknormen/archive/2008/04/16/asp-net-mvc-framework-2-a-step-by-step-guide-to-create-a-simple-web-application.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2008/04/16/asp-net-mvc-framework-2-a-step-by-step-guide-to-create-a-simple-web-application.aspx"&gt;step by step guide by using the Preview 2&lt;/A&gt; of the ASP.Net MVC Framework, the following is an updated version that targets the pre-release of the Preview 3 version of the ASP.Net MVC Framework.&lt;/P&gt;
&lt;P&gt;In this post you will learn how to use most of the new features in the pre-release of the Preview 3 version of the ASP.NET Framework, you will also get a basic understanding about how to use LINQ to SQL. This step by step guide shouldn't be used as a "best way to create and design" a web applications by using the ASP.NET MVC Framework, this guide should only be used to get started with the MVC Framework in a fast and easy way and learn different ways to use some of the features shipped with the ASP.Net MVC Framework pre-release Preview 3. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;To get the pre-release version, you can download it from &lt;A class="" href="http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=12640" target=_blank mce_href="http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=12640"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;In this step by step guide you are going to create a web based application where you will be able to list and modify customers. This step by step guide will use C# 3.0 and the Northwind database.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;1. Open Visual Studio 2008&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;2. Create a new Project and select the ”ASP.NET MVC Web Project” project from the "My Template" section and name it to “CustomerApp”. &lt;/P&gt;
&lt;P&gt;&lt;BR&gt;3. Open global.asax and walk through the code. In Global.asax you can specify your routes. In this step by step guide you will use the default settings.&lt;/P&gt;&lt;PRE class=code&gt;routes.MapRoute(
               &lt;SPAN style="COLOR: #a31515"&gt;"Default"&lt;/SPAN&gt;,                                              &lt;SPAN style="COLOR: green"&gt;// Route name
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;"{controller}/{action}/{id}"&lt;/SPAN&gt;,                           &lt;SPAN style="COLOR: green"&gt;// URL with parameters
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;{ controller = &lt;SPAN style="COLOR: #a31515"&gt;"Home"&lt;/SPAN&gt;, action = &lt;SPAN style="COLOR: #a31515"&gt;"Index"&lt;/SPAN&gt;, id = &lt;SPAN style="COLOR: #a31515"&gt;"" &lt;/SPAN&gt;}, &lt;SPAN style="COLOR: green"&gt;// Parameter defaults
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;{ controller = &lt;SPAN style="COLOR: #a31515"&gt;@"[^\.]*" &lt;/SPAN&gt;}                          &lt;SPAN style="COLOR: green"&gt;// Parameter constraints
           &lt;/SPAN&gt;);&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;As you can see, there is a new method added to the RouteCollection, MapRoute. This method can be used to make the configuration of routing more cleaner. You can still use the old way with the Route object.&lt;/P&gt;
&lt;P&gt;The first thing to do is to create your business objects (your Model) which your Controllers will interact with. In this step by step guide you will use LINQ to SQL, even if it’s not advisable to generate domain models out from a data source you will learn how to do it, only to get up and running fast.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: The business objects belongs to the Model in the MVC pattern.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;4. Right click on the Models folder in the Solution Explorer and select Add &amp;gt; New Item.&lt;/P&gt;
&lt;P&gt;Select the “LINQ to SQL Classes” template and name the file to “Northwind.dbml” and press the Add button.&lt;/P&gt;
&lt;P&gt;To create your Model out from a data source you need to add a database connection to Visual Studio. This can be done by using the Server Explorer. In the Solution Explorer, right click on the Data Connections node and select Add Connection. Enter the following information to connect to the Northwind database:&lt;/P&gt;
&lt;P&gt;Server Name: (local)&lt;/P&gt;
&lt;P&gt;Select the Northwind database, test the connection and press OK if you were able to connect to the database.&lt;/P&gt;
&lt;P&gt;A connection to the Northwind database will now be added under the “Data Connections” node in the Server Explorer. Expand the database and the Tables nodes; drag out the Customer table into the design view of the Northwind.dbml file. Save the file. When you save the file, LINQ to SQL will generate a Customer object for you where the columns from the Customers table will be added as properties to the Customer object. LINQ to SQL will also generate a DataContext object (NorthwindDataContext) which you will use later to access your information from the data source. Expand the Northwind.dbml file in the Solution Explorer, double click on the Northwind.designer.cs file and walk through the code. LINQ to SQL uses attributes to map classes to a table.&lt;/P&gt;
&lt;P&gt;Now it’s time to create the Controller which you are going to use in this application.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;5. Right click on the Controllers folder in the Solution Explorer and add a new item and select the “MVC Controller Class” template from the "My Templates" section, name it to “CustomerController”.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;6. Add a private field of the type NorhwindDataContext to the Controller and name the field to “northwindDataContext”. Set the field to an instance of the NorthwindDataContext object.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: The NorthwindDataContext class will exist in the CustomerApp.Models namespace.&lt;/I&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;using &lt;/SPAN&gt;CustomerApp.Models;

&lt;SPAN style="COLOR: blue"&gt;namespace &lt;/SPAN&gt;CustomerApp.Controllers
{
    &lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CustomerController &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;Controller&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IDisposable
    &lt;/SPAN&gt;{
        &lt;SPAN style="COLOR: blue"&gt;private &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext &lt;/SPAN&gt;northwindDataContext = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext&lt;/SPAN&gt;();
       
        &lt;SPAN style="COLOR: green"&gt;//...
    &lt;/SPAN&gt;}
}&lt;BR&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;7. Make sure the CustomerController implements the IDisposable interface, and implement the Dispose method and make sure it will make a call to the northwindDataContext’s Dispose method. This will make sure the NorthwindDataContext’s Dispose method will be called when the Controller have been used. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CustomerController &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;Controller&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IDisposable
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;private &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext &lt;/SPAN&gt;northwindDataContext = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext&lt;/SPAN&gt;();
   
    &lt;SPAN style="COLOR: green"&gt;//...
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;Dispose()
    {
        &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.northwindDataContext.Dispose();
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;8. Add an Action method and name it to “List”. The List action will retrieve all Customers from the Customers table of the Northwind database. LINQ to SQL will be used to get all the Customers, order the Customers by CompanyName. When all customers are retrieved make sure to render a View with the name “Customers” (The View will be added later to the project), the View should render a generic list of Customer objects. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ActionResult &lt;/SPAN&gt;List()
{
    &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;Customer&amp;gt; customers = (&lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;northwindDataContext.Customers
                                &lt;SPAN style="COLOR: blue"&gt;orderby &lt;/SPAN&gt;customer.CompanyName
                                &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;customer).ToList();

    &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;RenderView(&lt;SPAN style="COLOR: #a31515"&gt;"Customers"&lt;/SPAN&gt;, customers);
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;There are some changes to the Action methods between the Preview 2 and pre-release of Preview 3 of the ASP.Net MVC Framework. An Action method now returns a ActionResult. The RenderView method will now return a RenderViewResult, which inherits the ActionResult class. The changes are made to make it easier to create unit test without needing to create a lot of Mock objects, and also to make it easier in the future to support async. programming etc. You can read more about it on &lt;A href="http://weblogs.asp.net/scottgu/archive/2008/04/16/asp-net-mvc-source-refresh-preview.aspx"&gt;Scott Guthire's blog&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Note: By default if we make a call to RenderView, without passing the name of the View, it will use the name of the Action method as the View to render.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;The following example shows an example of how a unit test can look like to test the List action method:&lt;/P&gt;&lt;PRE class=code&gt;&lt;BR&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestClass&lt;/SPAN&gt;]
&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CusomerControllerTest
&lt;/SPAN&gt;{
    [&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]
    &lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;ListCustomerTest()
    {
        &lt;SPAN style="COLOR: #2b91af"&gt;CustomerController &lt;/SPAN&gt;custController = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CustomerController&lt;/SPAN&gt;();

        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;result = custController.List() &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;RenderViewResult&lt;/SPAN&gt;;

        &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.IsNotNull(result, &lt;SPAN style="COLOR: #a31515"&gt;"Expected a RenderViewResult"&lt;/SPAN&gt;);

        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;customers = result.ViewData &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;&amp;gt;;
        
        &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.IsNotNull(result.ViewName, &lt;SPAN style="COLOR: #a31515"&gt;"Customers"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"..."&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.IsNotNull(customers, &lt;SPAN style="COLOR: #a31515"&gt;"..."&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: green"&gt;//...
    &lt;/SPAN&gt;}
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;&lt;EM&gt;Note: There is a helper method of the Controller base class called Redirect, which can be used to do a redirect within a Action method. The Redirect method returns a HttpRedirectResult, which inherits from the ActionResult class. The RedirectToAction method now returns an ActionRedirectResult class.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;9. Create a View with the name “Customers” which will render the generic list of Customer objects. To add a View, right click on the Views folder and add a new folder with the name “Customer”. Right click on the Customer folder and add a new item, select the “MVC View Page” template from the "My Template" section and name the View to “Customers”. Make sure the View data inherits from ViewPage&amp;lt;List&amp;lt;CustomerApp.Models.Customer&amp;gt;&amp;gt;. This will make sure the Model which is passed as an argument to the RenderView method is a known and typed for the View. Open the file Customers.aspx.cs file, replace so the page inherits from ViewPage&amp;lt;List&amp;lt;CustomerApp.Models.Customer&amp;gt;&amp;gt; instead of ViewPage.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public partial class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customers &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;ViewPage&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;CustomerApp.Models.&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;&amp;gt;&amp;gt;
{
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;10. Add code to render a list of Customers to the View, make sure the CustomerID and ContactName are listed. Open the Customer.aspx file, add an inline-code block and use foreach to iterate through the Customers, use the ViewData property to get access to the generic list of Customer objects.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;BR&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;@ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Page &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Language&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="C#" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;AutoEventWireup&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="true" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;CodeBehind&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customers.aspx.cs" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Inherits&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerApp.Views.Customer.Customers" &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;DOCTYPE &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;html PUBLIC &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;

&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="http://www.w3.org/1999/xhtml" &amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;( &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;ViewData) { &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;            &lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;Html.Encode(customer.CustomerID) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;Html.Encode(customer.ContactName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; } &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;Make sure you have saved all files, compile and run the application. To the end of the URL add “Customer/List” and press enter. The URL will look something like this: &lt;A href="http://localhost:%3cport%20number%3e/Customer/List" mce_href="http://localhost:%3cport%20number%3e/Customer/List"&gt;http://localhost:&amp;lt;port number&amp;gt;/Customer/List&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Your application will now make sure the Controller “CustomerController” will be used and also the Action method “List” will be invoked. If everything works, you will now see a list of CustomerID and ContactName.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: The route that was defined in global.asax looks like this:&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;BR&gt;{controller}/{action}/{id}&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;BR&gt;When you enter a URL like “Customer/List”, the Customer will be the name of the Controller to be used, and the List the name of the Action method to be executed, the id in the route will be empty and will be ignored.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;11. Add a new Action method to the CustomerController and name it to “View”. The View method should have one argument with the name ID and the type of String. Make sure the method will use LINQ to SQL to get a Customer from the Customers table which matches the CustomerID passed as an argument. If a customer is not found, use the Redirect method and navigate to "/Errors/CustomerNotFound/&amp;lt;customer ID&amp;gt;". Make sure to render a View with the name “Customer”. The View should render a detail view of a Customer.&lt;SPAN style="COLOR: blue"&gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ActionResult &lt;/SPAN&gt;View(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;ID)
{
    Customer customer = (&lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;northwindDataContext.Customers
                         &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;c.CustomerID == ID
                         &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;c).FirstOrDefault();

    &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(customer == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;Redirect(&lt;SPAN style="COLOR: #a31515"&gt;"/Error/CustomerNotFound/" &lt;/SPAN&gt;+ ID);

    &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;RenderView(&lt;SPAN style="COLOR: #a31515"&gt;"Customer"&lt;/SPAN&gt;, customer);
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;EM&gt;Note: You will not implement a Controller or Views that will handle the error in this guide, it is used to demonstrate the Redirect method, and also how you can create a unit test that can check that the Action do the right thing when a CustomerID is passed to the Action method, and the customer can't be found. &lt;BR&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;The following example will show you for example how you can write a test that will check if the user can't be found and that the Action method will do a Redirect:&lt;/P&gt;&lt;PRE class=code&gt;&lt;BR&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]
&lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;ViewCustomerTest_Cant_Found_Customer()
{
    &lt;SPAN style="COLOR: #2b91af"&gt;CustomerController &lt;/SPAN&gt;custController = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CustomerController&lt;/SPAN&gt;();

    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;result = custController.View(&lt;SPAN style="COLOR: #a31515"&gt;"Unknow Id"&lt;/SPAN&gt;) &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;HttpRedirectResult&lt;/SPAN&gt;;

    &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.IsNotNull(result, &lt;SPAN style="COLOR: #a31515"&gt;"Expected a http redirect"&lt;/SPAN&gt;);

    &lt;SPAN style="COLOR: green"&gt;//...
&lt;/SPAN&gt;}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Because the Redirect method returns a HttpRedirectResult, you can in your Unit test, see if the Action method will do a Redirect or not.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;12. Add a new View to the Views/Customer folder and name it “Customer”. Make sure the Customer View will inherit form the ViewPage&amp;lt;CustomerApp.Models.Customer&amp;gt; class. &lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public partial class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;ViewPage&lt;/SPAN&gt;&amp;lt;CustomerApp.Models.&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;&amp;gt;
{
}&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;13. Open the Customer.aspx page and add inline-code block to show detail information about a Customer, make sure input text elements is used to display the content of the Customer (you will later in the lab update the Customer). Set the name attribute of the input text element to the same name as the property of the Customer object it should display. The CustomerID should be displayed as a simple text, and also be a value of an input hidden field with the name “CustomerID”. The reason why you will use a hidden field with the CustomerID, is because you will later use the MVC Frameworks “databinding” feature.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: The Customer object has several properties, and to make this lab simple you can only use some of them, CustomerID, CompanyName, ContactName and Phone.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;Leave the &amp;lt;Form&amp;gt; elements action attribute empty at the moment. &lt;BR&gt;&lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;@ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Page &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Language&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="C#" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;AutoEventWireup&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="true" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;CodeBehind&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customer.aspx.cs" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Inherits&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerApp.Views.Customer.Customer" &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;DOCTYPE &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;html PUBLIC &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;

&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="http://www.w3.org/1999/xhtml" &amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;form &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="post" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;action&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=""&amp;gt;
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Customer ID:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;= &lt;/SPAN&gt;Html.Encode(ViewData.CustomerID) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Company name:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CompanyName" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.CompanyName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Contact Name:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="ContactName" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.ContactName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Phone:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Phone" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.Phone) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;

        &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;

        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="hidden" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerID" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.CustomerID) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;
        
&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;form&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: Instead of writing the whole &amp;lt;input&amp;gt; element, you can also use the Html help method:&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;BR&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;= &lt;/SPAN&gt;Html.TextBox(&lt;SPAN style="COLOR: #a31515"&gt;"CompanyName"&lt;/SPAN&gt;, ViewData.CompanyName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;BR&gt;This method will render the following output:&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;BR&gt;&amp;lt;input type=”text” size=”20” name=”CompanyName” id=”CompanyName” value=”My Company”/&amp;gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;BR&gt;Writing HTML will give you more control over the HTML which will be sent to the client.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;Save your View and build your solution. Start the application and to the end of the URL enter “Customer/View/ALFKI”. The URL will look something like this: &lt;A href="http://localhost:%3cport%20number%3e/Customer/View/ALFKI" mce_href="http://localhost:%3cport%20number%3e/Customer/View/ALFKI"&gt;http://localhost:&amp;lt;port number&amp;gt;/Customer/View/ALFKI&lt;/A&gt;. The Customer controller will be used and its Action method View will be called based on the route in global.asax the value after the specified Action method will be passed as an argument to the specified Action method. The argument is mapped to the argument with the name “ID”.&lt;/P&gt;
&lt;P&gt;If everything works, you will now see a detail view over the Customer with the CustomerID “ALFKI”.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;14. Open the Customers.aspx View and make sure the listed CustomerID instead will be a HyperLink, which will have the following URL pattern: “/Customer/View/&amp;lt;CustomerID&amp;gt;”.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;BR&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;@ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Page &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Language&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="C#" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;AutoEventWireup&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="true" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;CodeBehind&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customers.aspx.cs" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Inherits&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerApp.Views.Customer.Customers" &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;DOCTYPE &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;html PUBLIC &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;

&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="http://www.w3.org/1999/xhtml" &amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;( &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;ViewData) { &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;            &lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;a &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;href&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="/Customer/View/&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;=customer.CustomerID &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;customer.CustomerID &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;a&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;Html.Encode(customer.ContactName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; } &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;Instead of writing the &amp;lt;a&amp;gt; element you can use the Html helper method to let it create an RouteLink, for example like this:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;PRE class=code&gt;&lt;BR&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;Html.RouteLink(ViewData.CustomerID, "Default", new { ID = ViewData.CustomerID, Controller="Home", Action="View" }) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;/SPAN&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;When you use the MapRoute method to create a route in global.asax, you give the route a name, for example the default route has the name "Default". When you use the RouteLink method, you can specify the name of the route, the RouteLink method generates a HyperLink. The example above will use the Default route. The result of the above example will be: &lt;BR&gt;&lt;BR&gt;&amp;lt;a href="http://weblogs.asp.net/Home/View/AFLK"&amp;gt;AFLK&amp;lt;/a&amp;gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Note: In a future release of the MVC Framework, you don't need to pass the Controller and the Action to the RouteLink.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;Make sure you have saved all files, compile and run the application. To the end of the URL add “Customer/List” and press enter. The URL will look something like this: &lt;A href="http://localhost:%3cport%20number%3e/Customer/List" mce_href="http://localhost:%3cport%20number%3e/Customer/List"&gt;http://localhost:&amp;lt;port number&amp;gt;/Customer/List&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;If everything works correct, you will now see a list of Customers and the CustomerID will not be a HyperLink. Press on a CustomerID and make sure it will navigate to the Customer View and display detail information about the selected Customer.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: The URL of the HyperLink’s href attribute does not point to a specific file, instead if will point to the Controller and the Action method to be executed when it’s pressed.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;15. Add a new Action method to the CustomerController and name it “Remove”. Make sure it takes an ID of type string as an argument. Use LINQ to SQL to remove a Customer with the CustomerID matches the value of the ID argument. To Remove a Customer you first need to retrieve the Customer object, and then use the method DeleteOnSubmit method of the Customers property of the NorthwindDataContext object. The DeleteOnSubmit takes a Customer object as an argument. To execute the Delete, you need to call the SubmitChanges method of the NorthwindDataContext object.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: Both the View method and the Remove method will get a specific Customer by CustomerID, so you can do some refactoring here and use Extract method to extract code which will get the Customer by CustomerID and put it into a new method, and reuse that method both in the View and Remove Action method. To use the Refactoring feature within Visual Studio, mark the code you want to extract to a method and right click on the selected code block. Select Refactoring and Extract method option. Give the new method a name and press the OK button.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;If you decide to use the Refactoring feature you can mark the following code in the View method:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;PRE class=code&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer &lt;/SPAN&gt;customer = (&lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;northwindDataContext.Customers
                     &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;c.CustomerID == ID
                     &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;c).FirstOrDefault();&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;/FONT&gt;&lt;BR&gt;Righ click on the selected code and select Refactoring and Extract method. Give it the name “GetCustomerByID” and press the OK button. If you get an alert box, just press “Yes” to ignore it.&lt;/P&gt;
&lt;P&gt;You should now have a new method with the name GetCustomerByID which the View method will make a call to.&lt;/P&gt;
&lt;P&gt;In the Remove method, make a call to the GetCustomerByID to get the Customer object to delete. If the application fails to remove a Customer, throw an exception. If the remove was successfully, make a call to RedirectToAction to redirect to the Action with the name “List”. This will make sure the List method of the CustomerController will be called.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ActionResult &lt;/SPAN&gt;Remove(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;ID)
{
    &lt;SPAN style="COLOR: #2b91af"&gt;Customer &lt;/SPAN&gt;customer = GetCustomerByID(ID);

    northwindDataContext.Customers.DeleteOnSubmit(customer);

    &lt;SPAN style="COLOR: blue"&gt;try
    &lt;/SPAN&gt;{
        northwindDataContext.SubmitChanges();
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;RedirectToAction(&lt;SPAN style="COLOR: #a31515"&gt;"List"&lt;/SPAN&gt;);
    }
    &lt;SPAN style="COLOR: blue"&gt;catch &lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;Exception &lt;/SPAN&gt;e)
    {
        &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ApplicationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;.Format(&lt;SPAN style="COLOR: #a31515"&gt;"Can't remove the Customer {0}"&lt;/SPAN&gt;, ID), e);
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;16. Next step is to add a HyperLink to the Customers View, and set the URL to “/Customer/Remove/&amp;lt;CustomerID&amp;gt;” for each Customer. The name of the HyperLink should be “Remove” and can be placed before the CustomerID HyperLink. When you press the HyperLink, the Remove method of the CustomerController will be called and it will remove the Customer.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;BR&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;@ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Page &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Language&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="C#" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;AutoEventWireup&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="true" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;CodeBehind&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customers.aspx.cs" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Inherits&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerApp.Views.Customer.Customers" &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;DOCTYPE &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;html PUBLIC &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;

&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="http://www.w3.org/1999/xhtml" &amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;( &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;ViewData) { &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;            &lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;a &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;href&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="/Customer/Remove/&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;=customer.CustomerID &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"&amp;gt;&lt;/SPAN&gt;Remove&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;a&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;&amp;amp;nbsp;
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;a &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;href&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="/Customer/View/&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;=customer.CustomerID &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;customer.CustomerID &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;a&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;Html.Encode(customer.ContactName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; } &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;Build the project and run the application, enter the URL to list Customers (By now you should know the URL). Press one of the Remove HyperLink. You will probably get a SQL Exception about reference problems. You aren’t going to resolve that exception. It will indicate that the remove was executed and in this lab you can be satisfied with that.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;17. Add an Update method to the CustomerController, make sure it takes a argument with the name customerID and of type String. Use the customerID to call the GetCustomerByID method to retrieve the Customer from the database. Update the Customer with the new information and use the RedirectToAction method to redirect to the “List” action.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: When using LINQ to SQL we need to get a Customer which is attached to a DataContext, which is the reason why you need to get the Customer. Another reason is also to make it easy to handle concurrency issues etc.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;Use the BindingHelperExtension class’s UpdateFrom method to set the Customer objects properties out from the Reuqest.Form collection.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: The BindingHelperExtension’s UpdateFrom method will get the value out from an input field with the same name as the property of the object passed to the UpdateFrom method and set the value to the object. In the Customer View, there were input fields with the same name as the properties of the Customer object, so those values will be “mapped” to the Customer object you will pass into the UpdateFrom method.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ActionResult &lt;/SPAN&gt;Update(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;customerID)
{
    &lt;SPAN style="COLOR: #2b91af"&gt;Customer &lt;/SPAN&gt;customer = GetCustomerByID(customerID);

    &lt;SPAN style="COLOR: #2b91af"&gt;BindingHelperExtensions&lt;/SPAN&gt;.UpdateFrom(customer, Request.Form);

    &lt;SPAN style="COLOR: blue"&gt;try
    &lt;/SPAN&gt;{
        northwindDataContext.SubmitChanges();
    }
    &lt;SPAN style="COLOR: blue"&gt;catch &lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;Exception &lt;/SPAN&gt;e)
    {
        &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ApplicationException&lt;/SPAN&gt;(
                &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;.Format(&lt;SPAN style="COLOR: #a31515"&gt;"Can't update customer {0}"&lt;/SPAN&gt;, customerID)
                , e);
    }

    &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;RedirectToAction(&lt;SPAN style="COLOR: #a31515"&gt;"List"&lt;/SPAN&gt;);
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: Values of input fields on a View which has the same name as the arguments of the Update action method will be passed as a value to the respective argument. So in this case you have an input hidden field with the name CustomerID on the View, the value of that field, will be passed as a value to the Update method. So instead of using the UpdateFrom method of the BindingHelperExtensions, you could use arguments for all input fields.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;If you want to learn more about the BindingHeperExtension class, you can read my post "&lt;A href="http://weblogs.asp.net/fredriknormen/archive/2008/03/13/asp-net-mvc-framework-2-the-bindinghelper-class-ui-mapper.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2008/03/13/asp-net-mvc-framework-2-the-bindinghelper-class-ui-mapper.aspx"&gt;ASP.Net MVC Framework 2 - The BindingHelperClass (UI-mapper)&lt;/A&gt;". &lt;/P&gt;
&lt;P&gt;&lt;BR&gt;18. Open the Customer View and change the &amp;lt;form&amp;gt; element’s action argument to call the Update method of the CustomerController “/Customer/Update”. Add a Submit button to the View and give it the text value “Update”. &lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;@ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Page &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Language&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="C#" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;AutoEventWireup&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="true" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;CodeBehind&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customer.aspx.cs" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Inherits&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerApp.Views.Customer.Customer" &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;DOCTYPE &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;html PUBLIC &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;

&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="http://www.w3.org/1999/xhtml" &amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;form &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="post" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;action&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="/Customer/Update"&amp;gt;
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Customer ID:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;= &lt;/SPAN&gt;Html.Encode(ViewData.CustomerID) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Company name:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CompanyName" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.CompanyName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Contact Name:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="ContactName" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.ContactName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Phone:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Phone" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.Phone) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;

        &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;

        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="hidden" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerID" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.CustomerID) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;
        
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="submit" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Update" /&amp;gt;
    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;form&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;Build the solution and run the application. Enter the URL to list all Customers “Customer/List” and select a Customer from the list. Update the Customer and press the Update button. When the Update button is pressed, the customer will be updated and you will be redirected to the List action so the List of Customer will be displayed.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: Instead of using an input field with the name CustomerID, you could also set the action attribute to “/Customer/Update/&amp;lt;the customerID&amp;gt;”.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;The last thing to do now is to create an ActionFilter for the Remove action method. You only want to make sure the user with the name “student” will make a call to the Remove action method.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;19. Add a new class to the Model folder. Name it “AuthorizedFilterAttribute”. Make sure it inherits from the base class ActionFilterAttribute. The ActionFilterAttribute is located in the namespace “System.Web.Mvc”.&lt;/P&gt;
&lt;P&gt;The ActionFilter can be used to perform pre- and post action. You can use FilterAction to cancel an execution of an Action method, or you can use it to log information etc. The ActionFilter can now in pre-release of the Preview 3 of the ASP.Net MVC Framework also get access to the ActionResult returned from the Action method. &lt;BR&gt;&lt;BR&gt;The ActionFilter AuthorizedFilter will be used to only make sure the user “student” has access to the Remove method, if not a SecurityException will be thrown.&lt;/P&gt;
&lt;P&gt;To implement code for a pre action, override the OnActionExecuting method of the ActionFilterAttribute class. To perform a post action, override the OnActionExecuted method. To get the current user you can use the filterContext arugment’s HttpContext property and its User.Identity.name property. You don’t need to cancel the action of the AuthorizedFilter when you throw an exception. To cancel, you can set the filterContext Cancel property to true.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;using &lt;/SPAN&gt;System.Security;

&lt;SPAN style="COLOR: blue"&gt;namespace &lt;/SPAN&gt;CustomerApp.Models
{
    &lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;AuthorizedFilter &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;ActionFilterAttribute
    &lt;/SPAN&gt;{
        &lt;SPAN style="COLOR: blue"&gt;public override void &lt;/SPAN&gt;OnActionExecuting(&lt;SPAN style="COLOR: #2b91af"&gt;FilterExecutingContext &lt;/SPAN&gt;filterContext)
        {
            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(filterContext.HttpContext.User.Identity.Name != &lt;SPAN style="COLOR: #a31515"&gt;"student"&lt;/SPAN&gt;)
                &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SecurityException&lt;/SPAN&gt;(&lt;BR&gt;                      &lt;SPAN style="COLOR: #a31515"&gt;"You aren't allowed to call this action!"&lt;/SPAN&gt;);
        }
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Apply the AuthorizedFilter to the Remove method. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;AuthorizedFilter&lt;/SPAN&gt;]
&lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;Remove(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;ID)&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;I&gt;&lt;BR&gt;Note: If you want to make sure the filter will be used for every Action method of a Controller, you can add the attribute to the Controller’s class.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;If you want to test if the filter works, add a breakpoint to the OnActionExecuting method and run the application in debug mode. List the Customers and press the Remove HyperLink, or you can simply run the application, list the customers and press the Remove Hyperink, it should probably give you a SecurityException. If you want to learn more about ActionFilter, you can read my post "&lt;A href="http://weblogs.asp.net/fredriknormen/archive/2008/03/12/asp-net-mvc-framework-2-interception-and-creating-a-role-action-filter.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2008/03/12/asp-net-mvc-framework-2-interception-and-creating-a-role-action-filter.aspx"&gt;ASP.Net MVC Framework 2 - Interception and creating a Role Action Filter&lt;/A&gt;", I will later create a new post about the ActionFilter in the pre-release of Preview 3.&lt;/P&gt;
&lt;P&gt;In this step by step guide you have learned how to create a simple application by using the ASP.Net MVC Framework pre-release of Preview 3. You have learned how to create a Controller, Views and also ActionFilter to intercept pre actions to a Action method.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6105638" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>ASP.Net MVC Framework Preview 2 - A step by step guide to create a simple Web Application</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/04/16/asp-net-mvc-framework-2-a-step-by-step-guide-to-create-a-simple-web-application.aspx</link><pubDate>Wed, 16 Apr 2008 08:50:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6103152</guid><dc:creator>Fredrik N</dc:creator><slash:comments>19</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6103152</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/04/16/asp-net-mvc-framework-2-a-step-by-step-guide-to-create-a-simple-web-application.aspx#comments</comments><description>&lt;P&gt;&lt;STRONG&gt;UPDATE: When I cerated a new post on my blog about the pre-release of Preview 3, I replaced this one with the new post. I did my best to recover the content. Sorry!&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;In this post you will learn how to use most of the new features in the Preview 2 version of the ASP.NET Framework, you will also get a basic understanding about how to use LINQ to SQL. This step by step guide shouldn't be used as a "best way to create and design" a web applications by using the ASP.NET MVC Framework, this guide should only be used to get started with the MVC Framework in a fast and easy way and learn different ways to use some of the features shipped with the ASP.Net MVC Framework Preview 2. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;To get the pre-release version, you can download it from here.&lt;/P&gt;
&lt;P&gt;In this step by step guide you are going to create a web based application where you will be able to list and modify customers. This step by step guide will use C# 3.0 and the Northwind database.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;1. Open Visual Studio 2008&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;2. Create a new Project and select the ”ASP.NET MVC Web Project” project and name it to “CustomerApp”. &lt;/P&gt;
&lt;P&gt;&lt;BR&gt;3. Open global.asax and walk through the code. In Global.asax you can specify your routes. In this step by step guide you will use the default settings.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;The first thing to do is to create your business objects (your Model) which your Controllers will interact with. In this step by step guide you will use LINQ to SQL, even if it’s not advisable to generate domain models out from a data source you will learn how to do it, only to get up and running fast.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: The business objects belongs to the Model in the MVC pattern.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;4. Right click on the Models folder in the Solution Explorer and select Add &amp;gt; New Item.&lt;/P&gt;
&lt;P&gt;Select the “LINQ to SQL Classes” template and name the file to “Northwind.dbml” and press the Add button.&lt;/P&gt;
&lt;P&gt;To create your Model out from a data source you need to add a database connection to Visual Studio. This can be done by using the Server Explorer. In the Solution Explorer, right click on the Data Connections node and select Add Connection. Enter the following information to connect to the Northwind database:&lt;/P&gt;
&lt;P&gt;Server Name: (local)&lt;/P&gt;
&lt;P&gt;Select the Northwind database, test the connection and press OK if you were able to connect to the database.&lt;/P&gt;
&lt;P&gt;A connection to the Northwind database will now be added under the “Data Connections” node in the Server Explorer. Expand the database and the Tables nodes; drag out the Customer table into the design view of the Northwind.dbml file. Save the file. When you save the file, LINQ to SQL will generate a Customer object for you where the columns from the Customers table will be added as properties to the Customer object. LINQ to SQL will also generate a DataContext object (NorthwindDataContext) which you will use later to access your information from the data source. Expand the Northwind.dbml file in the Solution Explorer, double click on the Northwind.designer.cs file and walk through the code. LINQ to SQL uses attributes to map classes to a table.&lt;/P&gt;
&lt;P&gt;Now it’s time to create the Controller which you are going to use in this application.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;5. Right click on the Controllers folder in the Solution Explorer and add a new item and select the “MVC Controller Class” template,&amp;nbsp; name it to “CustomerController”.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;6. Add a private field of the type NorhwindDataContext to the Controller and name the field to “northwindDataContext”. Set the field to an instance of the NorthwindDataContext object.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: The NorthwindDataContext class will exist in the CustomerApp.Models namespace.&lt;/I&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;using &lt;/SPAN&gt;CustomerApp.Models;

&lt;SPAN style="COLOR: blue"&gt;namespace &lt;/SPAN&gt;CustomerApp.Controllers
{
    &lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CustomerController &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;Controller&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IDisposable
    &lt;/SPAN&gt;{
        &lt;SPAN style="COLOR: blue"&gt;private &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext &lt;/SPAN&gt;northwindDataContext = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext&lt;/SPAN&gt;();
       
        &lt;SPAN style="COLOR: green"&gt;//...
    &lt;/SPAN&gt;}
}&lt;BR&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;7. Make sure the CustomerController implements the IDisposable interface, and implement the Dispose method and make sure it will make a call to the northwindDataContext’s Dispose method. This will make sure the NorthwindDataContext’s Dispose method will be called when the Controller have been used. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CustomerController &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;Controller&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IDisposable
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;private &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext &lt;/SPAN&gt;northwindDataContext = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext&lt;/SPAN&gt;();
   
    &lt;SPAN style="COLOR: green"&gt;//...
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;Dispose()
    {
        &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.northwindDataContext.Dispose();
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;8. Add an Action method and name it to “List”. The List action will retrieve all Customers from the Customers table of the Northwind database. LINQ to SQL will be used to get all the Customers, order the Customers by CompanyName. When all customers are retrieved make sure to render a View with the name “Customers” (The View will be added later to the project), the View should render a generic list of Customer objects. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public void &lt;/SPAN&gt;List()
{
    &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;Customer&amp;gt; customers = (&lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;northwindDataContext.Customers
                                &lt;SPAN style="COLOR: blue"&gt;orderby &lt;/SPAN&gt;customer.CompanyName
                                &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;customer).ToList();

    RenderView(&lt;SPAN style="COLOR: #a31515"&gt;"Customers"&lt;/SPAN&gt;, customers);
}&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;9. Create a View with the name “Customers” which will render the generic list of Customer objects. To add a View, right click on the Views folder and add a new folder with the name “Customer”. Right click on the Customer folder and add a new item, select the “MVC View Page” template from the "My Template" section and name the View to “Customers”. Make sure the View data inherits from ViewPage&amp;lt;List&amp;lt;CustomerApp.Models.Customer&amp;gt;&amp;gt;. This will make sure the Model which is passed as an argument to the RenderView method is a known and typed for the View. Open the file Customers.aspx.cs file, replace so the page inherits from ViewPage&amp;lt;List&amp;lt;CustomerApp.Models.Customer&amp;gt;&amp;gt; instead of ViewPage.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public partial class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customers &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;ViewPage&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;CustomerApp.Models.&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;&amp;gt;&amp;gt;
{
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;10. Add code to render a list of Customers to the View, make sure the CustomerID and ContactName are listed. Open the Customer.aspx file, add an inline-code block and use foreach to iterate through the Customers, use the ViewData property to get access to the generic list of Customer objects.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;BR&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;@ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Page &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Language&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="C#" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;AutoEventWireup&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="true" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;CodeBehind&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customers.aspx.cs" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Inherits&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerApp.Views.Customer.Customers" &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;DOCTYPE &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;html PUBLIC &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;

&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="http://www.w3.org/1999/xhtml" &amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;( &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;ViewData) { &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;            &lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;Html.Encode(customer.CustomerID) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;Html.Encode(customer.ContactName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; } &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;Make sure you have saved all files, compile and run the application. To the end of the URL add “Customer/List” and press enter. The URL will look something like this: &lt;A href="http://localhost:%3cport%20number%3e/Customer/List" mce_href="http://localhost:%3cport%20number%3e/Customer/List"&gt;http://localhost:&amp;lt;port number&amp;gt;/Customer/List&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Your application will now make sure the Controller “CustomerController” will be used and also the Action method “List” will be invoked. If everything works, you will now see a list of CustomerID and ContactName.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: The route that was defined in global.asax looks like this:&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;BR&gt;{controller}/{action}/{id}&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;BR&gt;When you enter a URL like “Customer/List”, the Customer will be the name of the Controller to be used, and the List the name of the Action method to be executed, the id in the route will be empty and will be ignored.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;11. Add a new Action method to the CustomerController and name it to “View”. The View method should have one argument with the name ID and the type of String. Make sure the method will use LINQ to SQL to get a Customer from the Customers table which matches the CustomerID passed as an argument. If a customer is not found throw an exception. Make sure to render a View with the name “Customer”. The View should ender a detail view of a Customer.&lt;SPAN style="COLOR: blue"&gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;View(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;ID)
{
    &lt;SPAN style="COLOR: #2b91af"&gt;Customer &lt;/SPAN&gt;customer = (&lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;northwindDataContext.Customers
                         &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;c.CustomerID == ID
                         &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;c).FirstOrDefault();

    &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(customer == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
        &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ApplicationException&lt;/SPAN&gt;(
                    &lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;.Format(&lt;SPAN style="COLOR: #a31515"&gt;"Can't find a Customer with the CustomerID {0}"&lt;/SPAN&gt;, ID));

    RenderView(&lt;SPAN style="COLOR: #a31515"&gt;"Customer"&lt;/SPAN&gt;, customer);
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;12. Add a new View to the Views/Customer folder and name it “Customer”. Make sure the Customer View will inherit form the ViewPage&amp;lt;CustomerApp.Models.Customer&amp;gt; class. &lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public partial class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;ViewPage&lt;/SPAN&gt;&amp;lt;CustomerApp.Models.&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;&amp;gt;
{
}&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;13. Open the Customer.aspx page and add inline-code block to show detail information about a Customer, make sure input text elements is used to display the content of the Customer (you will later in the lab update the Customer). Set the name attribute of the input text element to the same name as the property of the Customer object it should display. The CustomerID should be displayed as a simple text, and also be a value of an input hidden field with the name “CustomerID”. The reason why you will use a hidden field with the CustomerID, is because you will later use the MVC Frameworks “databinding” feature.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: The Customer object has several properties, and to make this lab simple you can only use some of them, CustomerID, CompanyName, ContactName and Phone.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;Leave the &amp;lt;Form&amp;gt; elements action attribute empty at the moment. &lt;BR&gt;&lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;@ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Page &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Language&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="C#" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;AutoEventWireup&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="true" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;CodeBehind&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customer.aspx.cs" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Inherits&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerApp.Views.Customer.Customer" &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;DOCTYPE &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;html PUBLIC &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;

&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="http://www.w3.org/1999/xhtml" &amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;form &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="post" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;action&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=""&amp;gt;
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Customer ID:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;= &lt;/SPAN&gt;Html.Encode(ViewData.CustomerID) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Company name:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CompanyName" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.CompanyName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Contact Name:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="ContactName" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.ContactName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Phone:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Phone" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.Phone) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;

        &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;

        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="hidden" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerID" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.CustomerID) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;
        
&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;form&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: Instead of writing the whole &amp;lt;input&amp;gt; element, you can also use the Html help method:&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;BR&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;= &lt;/SPAN&gt;Html.TextBox(&lt;SPAN style="COLOR: #a31515"&gt;"CompanyName"&lt;/SPAN&gt;, ViewData.CompanyName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;BR&gt;This method will render the following output:&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;BR&gt;&amp;lt;input type=”text” size=”20” name=”CompanyName” id=”CompanyName” value=”My Company”/&amp;gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;BR&gt;Writing HTML will give you more control over the HTML which will be sent to the client.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;Save your View and build your solution. Start the application and to the end of the URL enter “Customer/View/ALFKI”. The URL will look something like this: &lt;A href="http://localhost:%3cport%20number%3e/Customer/View/ALFKI" mce_href="http://localhost:%3cport%20number%3e/Customer/View/ALFKI"&gt;http://localhost:&amp;lt;port number&amp;gt;/Customer/View/ALFKI&lt;/A&gt;. The Customer controller will be used and its Action method View will be called based on the route in global.asax the value after the specified Action method will be passed as an argument to the specified Action method. The argument is mapped to the argument with the name “ID”.&lt;/P&gt;
&lt;P&gt;If everything works, you will now see a detail view over the Customer with the CustomerID “ALFKI”.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;14. Open the Customers.aspx View and make sure the listed CustomerID instead will be a HyperLink, which will have the following URL pattern: “/Customer/View/&amp;lt;CustomerID&amp;gt;”.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;BR&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;@ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Page &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Language&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="C#" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;AutoEventWireup&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="true" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;CodeBehind&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customers.aspx.cs" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Inherits&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerApp.Views.Customer.Customers" &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;DOCTYPE &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;html PUBLIC &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;

&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="http://www.w3.org/1999/xhtml" &amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;( &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;ViewData) { &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;            &lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;a &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;href&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="/Customer/View/&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;=customer.CustomerID &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;customer.CustomerID &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;a&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;Html.Encode(customer.ContactName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; } &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;Instead of writing the &amp;lt;a&amp;gt; element you can use the Html helper method to let it create an ActionLink, for example like this:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;BR&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;= &lt;/SPAN&gt;Html.ActionLink&amp;lt;CustomerApp.Controllers.&lt;SPAN style="COLOR: #2b91af"&gt;CustomerController&lt;/SPAN&gt;&amp;gt;(a =&amp;gt; a.View(customer.CustomerID), customer.CustomerID) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;Make sure you have saved all files, compile and run the application. To the end of the URL add “Customer/List” and press enter. The URL will look something like this: &lt;A href="http://localhost:%3cport%20number%3e/Customer/List" mce_href="http://localhost:%3cport%20number%3e/Customer/List"&gt;http://localhost:&amp;lt;port number&amp;gt;/Customer/List&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;If everything works correct, you will now see a list of Customers and the CustomerID will not be a HyperLink. Press on a CustomerID and make sure it will navigate to the Customer View and display detail information about the selected Customer.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: The URL of the HyperLink’s href attribute does not point to a specific file, instead if will point to the Controller and the Action method to be executed when it’s pressed.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;15. Add a new Action method to the CustomerController and name it “Remove”. Make sure it takes an ID of type string as an argument. Use LINQ to SQL to remove a Customer with the CustomerID matches the value of the ID argument. To Remove a Customer you first need to retrieve the Customer object, and then use the method DeleteOnSubmit method of the Customers property of the NorthwindDataContext object. The DeleteOnSubmit takes a Customer object as an argument. To execute the Delete, you need to call the SubmitChanges method of the NorthwindDataContext object.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: Both the View method and the Remove method will get a specific Customer by CustomerID, so you can do some refactoring here and use Extract method to extract code which will get the Customer by CustomerID and put it into a new method, and reuse that method both in the View and Remove Action method. To use the Refactoring feature within Visual Studio, mark the code you want to extract to a method and right click on the selected code block. Select Refactoring and Extract method option. Give the new method a name and press the OK button.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;If you decide to use the Refactoring feature you can mark the following code in the View method:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;PRE&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer &lt;/SPAN&gt;customer = (&lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;northwindDataContext.Customers
                     &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;c.CustomerID == ID
                     &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;c).FirstOrDefault();&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(customer == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
    &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ApplicationException&lt;/SPAN&gt;(
                &lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;.Format(&lt;SPAN style="COLOR: #a31515"&gt;"Can't find a Customer with the CustomerID {0}"&lt;/SPAN&gt;, ID));&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Righ click on the selected code and select Refactoring and Extract method. Give it the name “GetCustomerByID” and press the OK button. If you get an alert box, just press “Yes” to ignore it.&lt;/P&gt;
&lt;P&gt;You should now have a new method with the name GetCustomerByID which the View method will make a call to.&lt;/P&gt;
&lt;P&gt;In the Remove method, make a call to the GetCustomerByID to get the Customer object to delete. If the application fails to remove a Customer, throw an exception. If the remove was successfully, make a call to RedirectToAction to redirect to the Action with the name “List”. This will make sure the List method of the CustomerController will be called.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;Remove(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;ID)
{
    &lt;SPAN style="COLOR: #2b91af"&gt;Customer &lt;/SPAN&gt;customer = GetCustomerByID(ID);

    northwindDataContext.Customers.DeleteOnSubmit(customer);

    &lt;SPAN style="COLOR: blue"&gt;try
    &lt;/SPAN&gt;{
        northwindDataContext.SubmitChanges();
        RedirectToAction(&lt;SPAN style="COLOR: #a31515"&gt;"List"&lt;/SPAN&gt;);
    }
    &lt;SPAN style="COLOR: blue"&gt;catch &lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;Exception &lt;/SPAN&gt;e)
    {
        &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ApplicationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;.Format(&lt;SPAN style="COLOR: #a31515"&gt;"Can't remove the Customer {0}"&lt;/SPAN&gt;, ID), e);
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;16. Next step is to add a HyperLink to the Customers View, and set the URL to “/Customer/Remove/&amp;lt;CustomerID&amp;gt;” for each Customer. The name of the HyperLink should be “Remove” and can be placed before the CustomerID HyperLink. When you press the HyperLink, the Remove method of the CustomerController will be called and it will remove the Customer.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;BR&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;@ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Page &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Language&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="C#" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;AutoEventWireup&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="true" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;CodeBehind&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customers.aspx.cs" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Inherits&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerApp.Views.Customer.Customers" &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;DOCTYPE &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;html PUBLIC &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;

&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="http://www.w3.org/1999/xhtml" &amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;( &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;ViewData) { &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;            &lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;a &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;href&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="/Customer/Remove/&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;=customer.CustomerID &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"&amp;gt;&lt;/SPAN&gt;Remove&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;a&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;&amp;amp;nbsp;
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;a &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;href&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="/Customer/View/&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;=customer.CustomerID &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;customer.CustomerID &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;a&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;Html.Encode(customer.ContactName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; } &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;Build the project and run the application, enter the URL to list Customers (By now you should know the URL). Press one of the Remove HyperLink. You will probably get a SQL Exception about reference problems. You aren’t going to resolve that exception. It will indicate that the remove was executed and in this lab you can be satisfied with that.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;17. Add an Update method to the CustomerController, make sure it takes a argument with the name customerID and of type String. Use the customerID to call the GetCustomerByID method to retrieve the Customer from the database. Update the Customer with the new information and use the RedirectToAction method to redirect to the “List” action.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: When using LINQ to SQL we need to get a Customer which is attached to a DataContext, which is the reason why you need to get the Customer. Another reason is also to make it easy to handle concurrency issues etc.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;Use the BindingHelperExtension class’s UpdateFrom method to set the Customer objects properties out from the Reuqest.Form collection.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: The BindingHelperExtension’s UpdateFrom method will get the value out from an input field with the same name as the property of the object passed to the UpdateFrom method and set the value to the object. In the Customer View, there were input fields with the same name as the properties of the Customer object, so those values will be “mapped” to the Customer object you will pass into the UpdateFrom method.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;Update(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;customerID)
{
    &lt;SPAN style="COLOR: #2b91af"&gt;Customer &lt;/SPAN&gt;customer = GetCustomerByID(customerID);
    
    &lt;SPAN style="COLOR: #2b91af"&gt;BindingHelperExtensions&lt;/SPAN&gt;.UpdateFrom(customer, Request.Form);

    northwindDataContext.SubmitChanges();

    RedirectToAction(&lt;SPAN style="COLOR: #a31515"&gt;"List"&lt;/SPAN&gt;);
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: Values of input fields on a View which has the same name as the arguments of the Update action method will be passed as a value to the respective argument. So in this case you have an input hidden field with the name CustomerID on the View, the value of that field, will be passed as a value to the Update method. So instead of using the UpdateFrom method of the BindingHelperExtensions, you could use arguments for all input fields.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;If you want to learn more about the BindingHeperExtension class, you can read my post "&lt;A href="http://weblogs.asp.net/fredriknormen/archive/2008/03/13/asp-net-mvc-framework-2-the-bindinghelper-class-ui-mapper.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2008/03/13/asp-net-mvc-framework-2-the-bindinghelper-class-ui-mapper.aspx"&gt;ASP.Net MVC Framework 2 - The BindingHelperClass (UI-mapper)&lt;/A&gt;". &lt;/P&gt;
&lt;P&gt;&lt;BR&gt;18. Open the Customer View and change the &amp;lt;form&amp;gt; element’s action argument to call the Update method of the CustomerController “/Customer/Update”. Add a Submit button to the View and give it the text value “Update”. &lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;@ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Page &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Language&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="C#" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;AutoEventWireup&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="true" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;CodeBehind&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customer.aspx.cs" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Inherits&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerApp.Views.Customer.Customer" &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;DOCTYPE &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;html PUBLIC &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;

&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="http://www.w3.org/1999/xhtml" &amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;form &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="post" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;action&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="/Customer/Update"&amp;gt;
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Customer ID:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;= &lt;/SPAN&gt;Html.Encode(ViewData.CustomerID) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Company name:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CompanyName" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.CompanyName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Contact Name:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="ContactName" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.ContactName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Phone:&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Phone" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.Phone) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;

        &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;

        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="hidden" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerID" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;= Html.Encode(ViewData.CustomerID) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;" /&amp;gt;
        
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="submit" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Update" /&amp;gt;
    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;form&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;Build the solution and run the application. Enter the URL to list all Customers “Customer/List” and select a Customer from the list. Update the Customer and press the Update button. When the Update button is pressed, the customer will be updated and you will be redirected to the List action so the List of Customer will be displayed.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: Instead of using an input field with the name CustomerID, you could also set the action attribute to “/Customer/Update/&amp;lt;the customerID&amp;gt;”.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;The last thing to do now is to create an ActionFilter for the Remove action method. You only want to make sure the user with the name “student” will make a call to the Remove action method.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;19. Add a new class to the Model folder. Name it “AuthorizedFilterAttribute”. Make sure it inherits from the base class ActionFilterAttribute. The ActionFilterAttribute is located in the namespace “System.Web.Mvc”.&lt;/P&gt;
&lt;P&gt;The ActionFilter can be used to perform pre- and post action. You can use FilterAction to cancel an execution of an Action method, or you can use it to log information etc.&amp;nbsp; &lt;BR&gt;&lt;BR&gt;The ActionFilter AuthorizedFilter will be used to only make sure the user “student” has access to the Remove method, if not a SecurityException will be thrown.&lt;/P&gt;
&lt;P&gt;To implement code for a pre action, override the OnActionExecuting method of the ActionFilterAttribute class. To perform a post action, override the OnActionExecuted method. To get the current user you can use the filterContext arugment’s HttpContext property and its User.Identity.name property. You don’t need to cancel the action of the AuthorizedFilter when you throw an exception. To cancel, you can set the filterContext Cancel property to true.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;using &lt;/SPAN&gt;System.Security;

&lt;SPAN style="COLOR: blue"&gt;namespace &lt;/SPAN&gt;CustomerApp.Models
{
    &lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;AuthorizedFilter &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;ActionFilterAttribute
    &lt;/SPAN&gt;{
        &lt;SPAN style="COLOR: blue"&gt;public override void &lt;/SPAN&gt;OnActionExecuting(&lt;SPAN style="COLOR: #2b91af"&gt;FilterExecutingContext &lt;/SPAN&gt;filterContext)
        {
            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(filterContext.HttpContext.User.Identity.Name != &lt;SPAN style="COLOR: #a31515"&gt;"student"&lt;/SPAN&gt;)
                &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SecurityException&lt;/SPAN&gt;(&lt;BR&gt;                      &lt;SPAN style="COLOR: #a31515"&gt;"You aren't allowed to call this action!"&lt;/SPAN&gt;);
        }
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Apply the AuthorizedFilter to the Remove method. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;AuthorizedFilter&lt;/SPAN&gt;]
&lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;Remove(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;ID)&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;I&gt;&lt;BR&gt;Note: If you want to make sure the filter will be used for every Action method of a Controller, you can add the attribute to the Controller’s class.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;If you want to test if the filter works, add a breakpoint to the OnActionExecuting method and run the application in debug mode. List the Customers and press the Remove HyperLink, or you can simply run the application, list the customers and press the Remove Hyperink, it should probably give you a SecurityException. If you want to learn more about ActionFilter, you can read my post "&lt;A href="http://weblogs.asp.net/fredriknormen/archive/2008/03/12/asp-net-mvc-framework-2-interception-and-creating-a-role-action-filter.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2008/03/12/asp-net-mvc-framework-2-interception-and-creating-a-role-action-filter.aspx"&gt;ASP.Net MVC Framework 2 - Interception and creating a Role Action Filter&lt;/A&gt;".&lt;/P&gt;
&lt;P&gt;In this step by step guide you have learned how to create a simple application by using the ASP.Net MVC Framework Preview 2. You have learned how to create a Controller, Views and also ActionFilter to intercept pre actions to a Action method.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6103152" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>ASP.Net MVC Framework 2 - The Binding helper class (UI-mapper)</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/03/13/asp-net-mvc-framework-2-the-bindinghelper-class-ui-mapper.aspx</link><pubDate>Thu, 13 Mar 2008 13:57:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5960575</guid><dc:creator>Fredrik N</dc:creator><slash:comments>18</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=5960575</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/03/13/asp-net-mvc-framework-2-the-bindinghelper-class-ui-mapper.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this post I’m going to explain the use of the ASP.Net MVC Framework’s BindingHelperExtensions class and how to use it as what I call a UI mapper (map user input fields to an object). The BindingHelperExtensions has the following five methods:&lt;/P&gt;&lt;PRE class=code&gt;T ReadFromRequest&amp;lt;T&amp;gt;(&lt;SPAN style="COLOR: blue"&gt;this &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Controller &lt;/SPAN&gt;controller, &lt;SPAN style="COLOR: #2b91af"&gt;String &lt;/SPAN&gt;key)

&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;ReadFromRequest(&lt;SPAN style="COLOR: blue"&gt;this &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Controller &lt;/SPAN&gt;controller, &lt;SPAN style="COLOR: #2b91af"&gt;String &lt;/SPAN&gt;key)

&lt;SPAN style="COLOR: blue"&gt;void &lt;/SPAN&gt;UpdateFrom(&lt;SPAN style="COLOR: #2b91af"&gt;Object &lt;/SPAN&gt;value, &lt;SPAN style="COLOR: #2b91af"&gt;NameValueCollection &lt;/SPAN&gt;values)

&lt;SPAN style="COLOR: blue"&gt;void &lt;/SPAN&gt;UpdateFrom(&lt;SPAN style="COLOR: #2b91af"&gt;Object &lt;/SPAN&gt;value, &lt;SPAN style="COLOR: #2b91af"&gt;NameValueCollection &lt;/SPAN&gt;values, &lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;objectPrefix)

&lt;SPAN style="COLOR: blue"&gt;void &lt;/SPAN&gt;UpdateFrom(&lt;SPAN style="COLOR: #2b91af"&gt;Object &lt;/SPAN&gt;value, &lt;SPAN style="COLOR: #2b91af"&gt;NameValueCollection &lt;/SPAN&gt;values, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;[] keys)&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;I will not go deep into the ReadFromRequest method, but they can be used to get a value out from the Request object, such as a QueryString or Form field. They are also extensions method to the Controller class. In this post I will write about the UpdateFrom methods which can be used to easy map a View’s user input fields to an object. &lt;/P&gt;
&lt;P&gt;&lt;BR&gt;&lt;STRONG&gt;&lt;EM&gt;Note: The UpdateFrom methods aren’t extension methods.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;The UpdateFrom methods can be used inside of a Controller’s action method to update an object in the Model based on input values. For example if you have a Customer object and want to fill it with values out from the Request.From, you can do the following in your Controller: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Binding&lt;/SPAN&gt;.UpdateFrom(customer, Request.Form);&lt;/PRE&gt;
&lt;P&gt;In the examples in this post I will use an alias: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;using &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Binding &lt;/SPAN&gt;= System.Web.Mvc.&lt;SPAN style="COLOR: #2b91af"&gt;BindingHelperExtensions&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;I only use it to avoid using the name of the BindingHelperExtension class to get access to the UpdateFrom methods. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;The UpdateFrom will see if there is an input field in the Request.Form collection with the same name as the property of the object passed as an argument. If there is a match, the value from input filed will be set to the value of the object’s property with the same name. For example if you have an input filed like this: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CompanyName" .../&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;And if the Customer object you pass to the UpdateFrom method has a property with the same name; the value from the CompanyName field will be set to the Customer’s CompanyName property. If you have a View like this: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CompanyName" /&amp;gt;&lt;/SPAN&gt;&lt;BR&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="submit" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="City" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Create"/&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;The value of the submit button will be set to the Customer object if it has a property with the name City. So be careful what name you set to the input elements. This can of course be handled by using one of the other two UpdateFrom methods. For example the UpdateFrom that takes a objectPrefix as an argument, can be used to pass in a prefix of the input field names which should be collected from the values argument and be set to the value argument. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;void &lt;/SPAN&gt;UpdateFrom(&lt;SPAN style="COLOR: #2b91af"&gt;Object &lt;/SPAN&gt;value, &lt;SPAN style="COLOR: #2b91af"&gt;NameValueCollection &lt;/SPAN&gt;values, &lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;objectPrefix)&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;For example if you have input fields that you want to belong to a specific object in the Model, you can use an object prefix like this: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customer.CompanyName" /&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customer.ContactName" /&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Product.Description" /&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Note: You can also use "_" to sepereate the prefix from the property name "Customer_CompanyName".&lt;BR&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;BR&gt;When you use the UdateFrom method and you only want to map the values from the input fields with the prefix Customer to a Customer object passed to the UpdateFrom, you simply pass the “Customer” as an objectPrefix to the UpdateFrom method: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Binding&lt;/SPAN&gt;.UpdateFrom(customer, Request.Form, &lt;SPAN style="COLOR: #a31515"&gt;"Customer"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;The UpdateFrom will now only collect fields with the prefix “Customer”, and the names after the prefix will be mapped to properties of the Customer object with the same name. By using object prefix you can simply categorize input fields to be mapped to different objects. The last UpdateFrom method you can use is the one that takes an array of keys as an argument. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;void &lt;/SPAN&gt;UpdateFrom(&lt;SPAN style="COLOR: #2b91af"&gt;Object &lt;/SPAN&gt;value, &lt;SPAN style="COLOR: #2b91af"&gt;NameValueCollection &lt;/SPAN&gt;values, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;[] keys)&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;By passing a string array with keys, only the specified keys will be collected from the values argument collection and be set to the value object’s properties. By using keys and pass a Request.Form to the values argument you can decide which input fields you want to collect.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;&lt;STRONG&gt;&lt;EM&gt;Note: The values argument of the UpdateFrom takes an object of type NameValueCollection, so you pass for example pass the Request.Params collection if you want to get values from both QueryStrings and Forms. You can also pass your own NameValueCollection as you probably already have figured out ;)&lt;/EM&gt;&lt;/STRONG&gt; &lt;BR&gt;&lt;BR&gt;To end this post I will give you a simple example how to use the UpdateFrom and with LINQ to SQL to create a new Customer to the Northwind database. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CustomerController &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;Controller
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;private &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext &lt;/SPAN&gt;dbc = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext&lt;/SPAN&gt;();

    &lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;Index()
    {
       RenderView(&lt;SPAN style="COLOR: #a31515"&gt;"Customer"&lt;/SPAN&gt;);
    }

    &lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;New()
    {
      &lt;SPAN style="COLOR: #2b91af"&gt;Customer &lt;/SPAN&gt;customer = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;();

      &lt;SPAN style="COLOR: #2b91af"&gt;Binding&lt;/SPAN&gt;.UpdateFrom(customer, Request.Form, &lt;SPAN style="COLOR: #a31515"&gt;"Customer"&lt;/SPAN&gt;);

      dbc.Customers.InsertOnSubmit(customer);
      dbc.SubmitChanges();
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Customer.aspx &lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;@ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Page &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Language&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="C#" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;AutoEventWireup&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="true" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;CodeBehind&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customer.aspx.cs" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Inherits&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="MvcUpdateInsertExample.Views.Customer.Customer" &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;DOCTYPE &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;html PUBLIC &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;

&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="http://www.w3.org/1999/xhtml" &amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;form &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;action&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customer/New" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="post"&amp;gt;
        
        &lt;/SPAN&gt;Company name: &lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customer.CompanyName" /&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;br &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;/&amp;gt;
        &lt;/SPAN&gt;Contact name: &lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customer.ContactName" /&amp;gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;br &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;/&amp;gt;
        
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;input &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="submit" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Create"/&amp;gt;
        
        &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;form&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;I hope this post gave you some brief understanding about how you can use the BindingHelperExtensions class to easy handle user input and map it to an object in the Model.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5960575" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>ASP.Net MVC Framework 2 - Interception and creating a Role Action Filter</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/03/12/asp-net-mvc-framework-2-interception-and-creating-a-role-action-filter.aspx</link><pubDate>Wed, 12 Mar 2008 08:50:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5955433</guid><dc:creator>Fredrik N</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=5955433</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/03/12/asp-net-mvc-framework-2-interception-and-creating-a-role-action-filter.aspx#comments</comments><description>&lt;P&gt;With the ASP.Net MVC Framework Preview 2, we can use Action Filters. An Action Filter can be used to intercept actions before and after an Action method of a Controller is executed. By using Action Filters&amp;nbsp;you can for example add code that will check if&amp;nbsp;a user has the right permission to access a action method, or&amp;nbsp;you can use it to log messaged before and action is executed, and also after. An Action Filter is easy to apply to a Controller or its Action method. Just add the ActionFilter as an attribute to the class or specific method of a Controller. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;RoleFilter&lt;/SPAN&gt;(AllowRoles=&lt;SPAN style="COLOR: #a31515"&gt;"Admin,Everyone"&lt;/SPAN&gt;]
&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MyController
&lt;/SPAN&gt;{
    [MyFilter]
    &lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;Index()
    {
        …
        RenderView(&lt;SPAN style="COLOR: #a31515"&gt;"Index"&lt;/SPAN&gt;);
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;If you apply an Aciton Filter to the Controller class, it will be executed for every action method in the Controller, if you add Action Filter to a specific method, it will only be called when that specific method is executed. An Action Filter has two methods that&amp;nbsp;you can override and use, OnActionExecuting and OnActionExecuted. As you probably can get out from the names, the Executing method will be called before the action method is executed, and the Executed, after the action method is executed. I will write more about the both method later in this post. You can apply several Action Filters to a Controller. One thing you need to have in mind is the order they will be executed. Reflection is used to get the Action Filters out from a Controller, Reflection can’t promise to give items in the order which they are applied. To solve this we can use the Order property of an Action Filter. By using the Order property we can specify in which order the Action Filter should be executed. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;MyFilter&lt;/SPAN&gt;(Order=1)]
[&lt;SPAN style="COLOR: #2b91af"&gt;MyFilter2&lt;/SPAN&gt;(Order=2)]
[&lt;SPAN style="COLOR: #2b91af"&gt;MyFilter3&lt;/SPAN&gt;(Order=3)]
&lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;Index()&lt;/PRE&gt;
&lt;P&gt;The OnActionExecuting method, which is the method that will be executed before an Action method of a Controller is executed, has one argument, filterContext of type FilterExecutingContext. The FilterExecutingContext has the following properties: &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;ActionMethod &lt;BR&gt;Cancel &lt;BR&gt;Controller &lt;BR&gt;HttpContext &lt;BR&gt;RouteData &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;The ActionMethod is of type MemberInfo and can give you all the information needed of the Action method that are going to be executed. The Cancel property is a Boolean where you can set it to true to cancel all execution of Action Filters and avoid calling the Action method. The Controller property is of type IController and will give you access to the current Controller. The HttpContext is of type HttpContextBase and will give you access to the HttpContext. The RouteData will give you access to the current route information. The OnActionExecuted method takes an argument of type FilterExecutedContext, which have the same properties as the FilterExecutingContex except Cancel and have two other properties, Exception which can be used to get an Exception thrown inside of an Action method, ExceptionHandled can be used to specify if you have handled the exception or not. The reason why the FilterExecutedContext don’t have the Cancel property is because you can’t cancel an action method that has already been executed. If you want a common way to handle exceptions thrown from an Action method, you can use the OnActionExecuted method to log or handle the exception in your own way, instead of showing the ASP.Net yellow error page. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;The Controller base class has two methods which can also be used to handle pre and post action for every action methods of the Controller.&amp;nbsp; They have the same name and interface as the Action Filter’s OnActionExecuting and OnActionExecuted methods. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MyController &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;Controller
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;public override void &lt;/SPAN&gt;OnActionExecuting(&lt;SPAN style="COLOR: #2b91af"&gt;FilterExecutingContext &lt;/SPAN&gt;filterContext)
    {
    }

    &lt;SPAN style="COLOR: blue"&gt;public override void &lt;/SPAN&gt;OnActionExecuted(&lt;SPAN style="COLOR: #2b91af"&gt;FilterExecutedContext &lt;/SPAN&gt;filterContext)
    {
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;If you need to use some unique logic for a specific Controller on every action that is called, you can override the OnActionExecuting and Executed methods of the Controller, if not, it’s better to use an Action Filter, so you can reuse code among the Controllers. &lt;BR&gt;The execution of an Action method is the following when an Action filter is used: &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;Controller.OnActionExecuting &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;ActionFilter.OnActionExecuting &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;Controller’s Action method Executed &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;ActionFilter.OnActionExecuted &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;Controller.OnActionExecuted &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;To implement an Action Filter you need to create a class which inherits from the ActionFilterAttribute, where you override the ActionFilterAttribute’s OnActionExectuing or Executed methods or both if you want to use both pre and post actions: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MyFilterAttribute &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;ActionFilterAttribute
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;public override void &lt;/SPAN&gt;OnActionExecuting(&lt;SPAN style="COLOR: #2b91af"&gt;FilterExecutingContext &lt;/SPAN&gt;filterContext)
    {
    }

    &lt;SPAN style="COLOR: blue"&gt;public override void &lt;/SPAN&gt;OnActionExecuted(&lt;SPAN style="COLOR: #2b91af"&gt;FilterExecutedContext &lt;/SPAN&gt;filterContext)
    {
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;When the Action Filter is created you can simply apply the attribute to a Controller’s Class definition (if you want the pre and post action to be executed for every action method) or to methods (if you only want to make sure the pre and post action should be used on a particular method). &lt;BR&gt;The following Action Filter can be used to make sure that only a user to a specified role can execute the action method. In this code the Roles feature of ASP.Net is used:&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;using &lt;/SPAN&gt;System;
&lt;SPAN style="COLOR: blue"&gt;using &lt;/SPAN&gt;System.Web.Mvc;
&lt;SPAN style="COLOR: blue"&gt;using &lt;/SPAN&gt;System.Web.Security;
&lt;SPAN style="COLOR: blue"&gt;using &lt;/SPAN&gt;System.Security;

&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;RoleFilterAttribute &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;ActionFilterAttribute
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;public string &lt;/SPAN&gt;Roles { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }


    &lt;SPAN style="COLOR: blue"&gt;public override void &lt;/SPAN&gt;OnActionExecuting(&lt;SPAN style="COLOR: #2b91af"&gt;FilterExecutingContext &lt;/SPAN&gt;filterContext)
    {
        &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;[] userRoles = System.Web.Security.&lt;SPAN style="COLOR: #2b91af"&gt;Roles&lt;/SPAN&gt;.GetRolesForUser();

        &lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;definedRole &lt;SPAN style="COLOR: blue"&gt;in this&lt;/SPAN&gt;.Roles.Split(&lt;SPAN style="COLOR: #a31515"&gt;','&lt;/SPAN&gt;))
        {
            &lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;role &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;userRoles)
            {
                &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(definedRole.Equals(role))
                    &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt;;
            }
        }

        &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SecurityException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Access not granted!"&lt;/SPAN&gt;);
    }

}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;When the attribute is created, you can simply add it to the Controller. &lt;/P&gt;&lt;PRE class=code&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;RoleFilter&lt;/SPAN&gt;(Roles=&lt;SPAN style="COLOR: #a31515"&gt;"Admin"&lt;/SPAN&gt;)]
&lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;ListUser()
{
    &lt;SPAN style="COLOR: green"&gt;//...
    &lt;/SPAN&gt;RenderView(&lt;SPAN style="COLOR: #a31515"&gt;"Users"&lt;/SPAN&gt;);
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The only thing I want now for make it easier to add or remove interceptors, is to make it possible to set them up in a configuration file. In that way we can add or remove interceptors from a Controller without recompile our application. We can use Spring.Net’s Aspect Oriented Programming (AOP) support to add interceptors, but it would be so nice to have AOP support added to C# ;)&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5955433" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>ASP.Net MVC Framework 2 - Define Routes in Web.config</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/03/11/asp-net-mvc-framework-2-define-routes-in-web-config.aspx</link><pubDate>Tue, 11 Mar 2008 15:06:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5951130</guid><dc:creator>Fredrik N</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=5951130</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/03/11/asp-net-mvc-framework-2-define-routes-in-web-config.aspx#comments</comments><description>&lt;P&gt;Something I like with ASP.Net MVC Framework Preview 2 is the RouteValueDictionary used by the Route class’s Defaults, Constraints and DataTokens property, before they use an anonymous type. Because the properties are now a specific type it’s much easier to define the routes in web.config. Before it was quite easy but some reflection was needed. I have now created a ConfigSection and a helper method to easily register routes which can be defined in the web.config for the Preview 2 version of the ASP.Net MVC Framework. I decided to use the following XML structure to define routes in web.config:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;routeTable&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
  &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;routes&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
  &lt;BR&gt;   &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;add &lt;BR&gt;      &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Microsoft&lt;/SPAN&gt;" &lt;BR&gt;      &lt;SPAN style="COLOR: red"&gt;url&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;{controller}/{action}/{id}&lt;/SPAN&gt;" &lt;BR&gt;      &lt;SPAN style="COLOR: red"&gt;routeHandlerType&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;MyMvcRouteHandler&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
      &lt;BR&gt;      &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;defaults &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;action&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Index&lt;/SPAN&gt;" &lt;SPAN style="COLOR: red"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;""&lt;SPAN style="COLOR: blue"&gt;/&amp;gt;
      &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;constraints &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;...&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;/&amp;gt;
      &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;dataTokens&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;/&amp;gt;
    &lt;BR&gt;   &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;add&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &lt;BR&gt;    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;add &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Default&lt;/SPAN&gt;" &lt;SPAN style="COLOR: red"&gt;url&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Default.aspx&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
      &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;defaults &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;controller&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Home&lt;/SPAN&gt;" &lt;SPAN style="COLOR: red"&gt;action&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Index&lt;/SPAN&gt;" &lt;SPAN style="COLOR: red"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;""&lt;SPAN style="COLOR: blue"&gt;/&amp;gt;
    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;add&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
  &lt;BR&gt;  &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;routes&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;routeTable&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;In my solution I also decided to make sure the “name” attribute of a route &amp;lt;add&amp;gt; is required and used as a key. The two other attributes of the route &amp;lt;add&amp;gt; are “url” and “routeHandlerType”, used to specify the Url and the type of a IRouteHandler to use (by default if it's empty the MvcRouteHandler will be used). I decided to use three child elements to the &amp;lt;add&amp;gt; element, &amp;lt;defaults&amp;gt;, &amp;lt;constraints&amp;gt; and &amp;lt;dataTokens&amp;gt;. Each attribute added to those elements will be added to the property of the Route class with the same name as the element and as a key, value dictionary of type RouteValueDictionary. To make it easy to register and read the routeTable out from the web.config, I created a helper class “RouteTableManager”. By calling its static method “RegisterRoute” from Application_Start in global.asax, the routes were registered. &lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;RouteTableManager&lt;/SPAN&gt;.RegisterRoutes(&lt;SPAN style="COLOR: #2b91af"&gt;RouteTable&lt;/SPAN&gt;.Routes);&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Or &lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;RouteTableManager&lt;/SPAN&gt;.RegisterRoutes();&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The last method will use the RouteTable class internally. &lt;/P&gt;
&lt;P&gt;By creating a custom MvcRouteModule and override the Init method, the routes can be automatically registered without using global.asax. &lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MyUrlRouteingModule &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;UrlRoutingModule
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;protected override void &lt;/SPAN&gt;Init(&lt;SPAN style="COLOR: #2b91af"&gt;HttpApplication &lt;/SPAN&gt;application)
    {
        &lt;SPAN style="COLOR: #2b91af"&gt;RouteTableManager&lt;/SPAN&gt;.RegisterRoutes();
        &lt;SPAN style="COLOR: blue"&gt;base&lt;/SPAN&gt;.Init(application);
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;You can download the whole source code from &lt;A href="http://weblogs.asp.net/blogs/fredriknormen/MvcConfigRoute.zip" mce_href="http://weblogs.asp.net/blogs/fredriknormen/MvcConfigRoute.zip"&gt;here&lt;/A&gt;.. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Note: The code will only read the routes from the Configuration file, you can’t use it to modify or add new routes. The idea is only to define the routes in a config file instead of adding them manually in the global.asax’s Application_Start event. I also put the source code into the Model directory of the ASP.Net MVC Project, it should be separated into an own assembly, but I was lazy ;)&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5951130" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>ASP.Net MVC Framework - Using Forms Authentication</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/02/07/asp-net-mvc-framework-using-forms-authentication.aspx</link><pubDate>Thu, 07 Feb 2008 08:59:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5728354</guid><dc:creator>Fredrik N</dc:creator><slash:comments>18</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=5728354</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/02/07/asp-net-mvc-framework-using-forms-authentication.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;&lt;STRONG&gt;NOTE: THIS Post was written when ASP.NET MVC Framework was in a early Preview stage.&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;This post is about using Forms Authentication with the MVC Framework. I only want to let you know that this is a simple and fast “hack” only to show a simple solution to get it work. I decided to use the Membership feature for the validation of user name and password.&lt;BR&gt;&lt;BR&gt;The first thing we need to do is to enable Forms Authentication and specify a Login Url, I decided to use the Login URL in a MVC “friendly” format:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;lt;authentication mode="Forms"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; &amp;lt;forms loginUrl="Login/Login"&amp;gt;&amp;lt;/forms&amp;gt;&lt;BR&gt;&amp;lt;/authentication&amp;gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;The value of the loginUrl has some redundant value, Login/Login, but this is because the first Login points to the LoginController and the last Login to the Action method Login in the LoginController.&lt;BR&gt;&lt;BR&gt;The next thing we need to do is to make sure anonymous users don’t have access to our site, this is done by adding the authorization section to the web.config:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;lt;authorization&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;deny users="?"/&amp;gt;&lt;BR&gt;&amp;lt;/authorization&amp;gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;If a user now try to get access to our site, he/she will be redirected to the ”Login/Login” URL, this will happen if we&amp;nbsp;use the following&amp;nbsp;URL format in the RouteTable:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;Url = "[controller]/[action]"&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;A call to&amp;nbsp;the Login Action method of the LoginController will take place.&lt;/P&gt;
&lt;P mce_keep="true"&gt;In the Views folder we can add our Login View (We will look at the LoginController later in this post).&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;/Views/Login/Login.aspx&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;The following is a simple Login View:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="MvcApplication4.Views.Login.Login" %&amp;gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "&lt;A href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" mce_href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&lt;/A&gt;"&amp;gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;lt;html xmlns="&lt;A href="http://www.w3.org/1999/xhtml" mce_href="http://www.w3.org/1999/xhtml"&gt;http://www.w3.org/1999/xhtml&lt;/A&gt;" &amp;gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;lt;head runat="server"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;&lt;BR&gt;&amp;lt;/head&amp;gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;lt;body&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;div&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;% if (this.ViewData.ContainsDataItem("LoginFaild")) { %&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;div style="color:Red"&amp;gt;&amp;lt;%=this.ViewData["LoginFaild"] %&amp;gt;&amp;lt;/div&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;% } %&amp;gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;form method="post" action="/Login/Login"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; User name: &amp;lt;input type="text" name="userName" /&amp;gt;&amp;lt;br /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Password:&amp;amp;nbsp; &amp;lt;input type="password" name="password" /&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;input type="submit" value="Login" /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/form&amp;gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/div&amp;gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;lt;/body&amp;gt;&lt;BR&gt;&amp;lt;/html&amp;gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;If the ViewData in will contain an data item with the name LoginFaild it will display the value added to the LingFaild data item, in this case a message like “Login faild….”&lt;/P&gt;
&lt;P mce_keep="true"&gt;The &amp;lt;form&amp;gt; element’s action method will make sure the LoginController’s Login method will be called when the user hit the Login submit button. As you can see the same URL specified for the FormsAuthentication’s loginUrl is used here. It’s because the &amp;lt;location&amp;gt; section in the web.config will not work properly with the MVC Framework "friendly" URL, so if we try to use another URL like “/Login/Authenticate” etc, we will be redirected to the “Login/Login” URL because the &amp;lt;authorization&amp;gt; section denies all anonymous user to access any URL in our app, besides the specified loginUrl.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;STRONG&gt;&lt;EM&gt;Note: The &amp;lt;location&amp;gt; will still work for&amp;nbsp;CSS files etc. So to enable a css style when we deny anonymouse use to access "files" in our web site, we can add the &amp;lt;location&amp;gt; to make sure CSS files and images etc can be requested.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;&amp;nbsp; &amp;lt;location path="Stylesheet1.css"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;system.web&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;authorization&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;allow users="*"/&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/authorization&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/system.web&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;/location&amp;gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;The username and password input fields values will be passed as arguments to the Login action of the LoginController. Here is the implementation of the LoginController and its Login action:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public class LoginController : Controller&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [ControllerAction]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Login(string userName, string password, string ReturnUrl)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (this.IsValidLoginArgument(userName, password))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (Membership.ValidateUser(userName, password))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.RedirectFromLoginPage(userName, ReturnUrl);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.ViewData["LoginFaild"] = "Login faild! Make sure you have entered the right user name and password!";&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RenderView("Login");&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private void RedirectFromLoginPage(string userName, string ReturnUrl)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FormsAuthentication.SetAuthCookie(userName, false);&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!string.IsNullOrEmpty(ReturnUrl))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Response.Redirect(ReturnUrl);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Response.Redirect(FormsAuthentication.DefaultUrl);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private bool IsValidLoginArgument(string userName, string password)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return !(string.IsNullOrEmpty(userName) &amp;amp;&amp;amp; string.IsNullOrEmpty(password));&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;As you can see the Login action method takes three arguments, the userName, password and ReturnUrl. The value for the userName and password, will came from the input fields located on the Login View, the ReturnUrl is a query string used by the FormsAuthentication, so the value of the ReutnrUrl argument will came from the ReturnUrl query string. If we have input fields with the same name as the arguments of an action method in the View, the value of the fields will be “mapped” to the argument of the action method with the same name as the field, the same thing with query strings.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;STRONG&gt;&lt;EM&gt;Note: There is a bug in the current CTP of the MVC Framework, so the value of a query string will not always be passed to the action method’s arguments.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;The firs lines of code in the Login action method will check if the username and password aren’t empty. The check is used to make sure the Login View will be displayed when FormsAuthentication will redirect to the LoginURL “Login/Login”. If a user tries to enter and URL and the user is an anonymous user (Not authenticated), the Login Action method of the LoginController will be executed. In this case the username and password argument will be null, and the Login View will be rendered. When the Login view is displayed, the user can now enter a username and a password and hit the Login button. When he/she have done that, the LoginController’s Action method will be called, and this time the username and password will not be empty, so the Membership feature will validate the user, if he/she is valid, the Authentication Cookie used by the FormsAutehtnicon will be set, and the user will be redirected to the URL he/she have requested, and the user is now authenticated and have access to the site.&lt;/P&gt;
&lt;P mce_keep="true"&gt;If the user enter a invalid user name or password, the Login View will be displayed and the “Login faild!.....” text added to the ViewData will be displayed.&lt;/P&gt;
&lt;P mce_keep="true"&gt;This was a simple solution to use the Forms Authentication, there are some other options where can simply create our own&amp;nbsp;RouteHandler which can handle the authentication check and redirect to a login View, or we can handle it on a Controller level. I will probably write a blog post about that later.&lt;/P&gt;
&lt;P mce_keep="true"&gt;I have another post about the MVC Framework and security: &lt;A href="http://weblogs.asp.net/fredriknormen/archive/2007/11/25/asp-net-mvc-framework-security.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2007/11/25/asp-net-mvc-framework-security.aspx"&gt;http://weblogs.asp.net/fredriknormen/archive/2007/11/25/asp-net-mvc-framework-security.aspx&lt;/A&gt;. The post is created on the pre-CTP of the MVC Framework, so have that in mind, but basically the solution is the same.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5728354" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>ASP.Net MVC Framework - Assign a ViewFactory to a Controller</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/01/30/asp-net-mvc-framework-assign-a-viewfactory-to-a-controller.aspx</link><pubDate>Wed, 30 Jan 2008 06:21:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5674763</guid><dc:creator>Fredrik N</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=5674763</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/01/30/asp-net-mvc-framework-assign-a-viewfactory-to-a-controller.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;I have seen several posts about how to assign a ViewFactory to a Controller, most of the case the code looks like this:&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;public class HomeController : Controller&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public HomeController()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.ViewFactory = new MyViewFactory();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;BR&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;The ViewFactory is assigned into the constructor of a Controller. If we want to use the same ViewFactroy for all Controllers in out app, we can for example create a new base class for our Controller:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;public class MyController : Controller&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public MyContoroller()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.ViewFactory = new MyViewFactory();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/P&gt;
&lt;P mce_keep="true"&gt;public class HomeController : MyController&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public HomeController() : base()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;I’m a more fan of making this configurable via a configuration file, to map a ViewFacotry to a Controller, this can for example be done with Spring.Net:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;&amp;lt;?xml version="1.0" encoding="utf-8" ?&amp;gt;&lt;BR&gt;&amp;lt;objects xmlns="&lt;A href="http://www.springframework.net/"&gt;http://www.springframework.net&lt;/A&gt;"&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns:xsi="&lt;A href="http://www.w3.org/2001/XMLSchema-instance"&gt;http://www.w3.org/2001/XMLSchema-instance&lt;/A&gt;"&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; xsi:schemaLocation="&lt;A href="http://www.springframework.net/"&gt;http://www.springframework.net&lt;/A&gt; &lt;A href="http://www.springframework.net/xsd/spring-objects.xsd"&gt;http://www.springframework.net/xsd/spring-objects.xsd&lt;/A&gt;"&amp;gt;&lt;BR&gt;&lt;BR&gt;&amp;nbsp; &amp;lt;object id="HomeController" type="MvcApplication.Controllers.HomeController, MvcApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;constructor-arg name="viewFactory" ref="MyFactory"/&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;/object&amp;gt;&lt;BR&gt;&lt;BR&gt;&amp;nbsp; &amp;lt;object id="MyFactory" type="MyViewFactory, MvcApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/&amp;gt;&lt;BR&gt;&amp;nbsp; &lt;BR&gt;&amp;lt;/objects&amp;gt;&lt;BR&gt;&lt;BR&gt;&lt;EM&gt;&lt;STRONG&gt;&lt;BR&gt;Note: You can read my other post about &lt;A class="" href="http://weblogs.asp.net/fredriknormen/archive/2007/11/17/asp-net-mvc-framework-create-your-own-icontrollerfactory-and-use-spring-net.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2007/11/17/asp-net-mvc-framework-create-your-own-icontrollerfactory-and-use-spring-net.aspx"&gt;using Spring.Net to create Controllers&lt;/A&gt;.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;public class HomeController : Controller&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp; public HomeController() : this(new DefaultViewFactory ()) {}&lt;BR&gt;&amp;nbsp;&amp;nbsp; public HomeController(IViewFactory viewFactory)&lt;BR&gt;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.ViewFactory = viewFactory;&lt;BR&gt;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;There a so many different solutions to set a ViewFactory during creation of a Controller, for example by creating an own ControllerFactory, the following code is from &lt;A class="" href="http://weblogs.asp.net/fredriknormen/archive/2007/11/18/asp-net-mvc-framework-create-your-own-iroutehandler.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2007/11/18/asp-net-mvc-framework-create-your-own-iroutehandler.aspx"&gt;my post about writing your own IRoutHandler&lt;/A&gt;:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;public IController CreateController(RequestContext context, string controllerName)&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //I should refactor out&amp;nbsp;the following two lines to not only make sure to make the&amp;nbsp;creation of the ObjectFactory once.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IResource input = new FileSystemResource(context.HttpContext.Request.MapPath("objects.xml"));&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IObjectFactory factory = new XmlObjectFactory(input);&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IController controller = (IController)factory.GetObject(controllerName, typeof(IController));&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (typeof(Controller).IsAssignableFrom(controller.GetType()))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; N2MVCConfigurationHandler config = ConfigurationManager.GetSection("Nsquared2/N2MVCSection") as N2MVCConfigurationHandler;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((Controller)controller).ViewFactory = N2ViewFactoryBuilder.GetViewFactory(config.ViewFactory);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return controller;&lt;BR&gt;}&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;What this code does, is to see if the Controller is assigned from the Controller class, ugly code, I know, but it’s only the Controller base class that has the ViewFactory property, and we can create a Controller which only implements the IController interface, and this interface don’t have the ViewFactory property.&lt;/P&gt;
&lt;P mce_keep="true"&gt;The code above will get the ViewFactory from a configuration file:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;&amp;lt;Nsquared2&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;N2MVCSection&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; controllerFactory="MvcApplication.Models.N2ControllerFactory, MvcApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; viewFactory="MvcApplication.Models.N2ViewFactory, MvcApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/&amp;gt;&lt;BR&gt;&amp;lt;/Nsquared2&amp;gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;In this example I assume that every Controller use the same ViewFactory, and that may not always be the case, so in that case the earlier example by using Spring.Net an inject a ViewFactory would be better use, or creating a new ControllerFactory which will map a Controller to a ViewFactory, so when the ControllerFactory creates the Controller, the factory could look up in a config file to see, which ViewFactory should be used for the specific Controller, for example:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;&amp;lt;Controllers defaultViewFactory=”myViewFactory”&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;add name=”HomeController” ViewFactory=”myOwnViewFactory” …&amp;gt;&lt;BR&gt;&amp;lt;/Controllers&amp;gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;What is the main goal with these kinds of solutions? Well we can now simply change the ViewFactory for our Controller without recompiling our code, and we will also remove the dependency from a Controller and the ViewFactory by not hard code the instantiation within the Controllers. I prefer Interface Driven Programming and the use of factories, and the possibility to easy configure the use of classes etc in a configuration file. That is why I also loved the Provider based solution that was shipped with ASP.Net 2.0, like the Membership and Roles features etc.&lt;BR&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5674763" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>MVC - Expression Language and custom tags</title><link>http://weblogs.asp.net/fredriknormen/archive/2007/11/27/mvc-expression-language-and-custom-tags.aspx</link><pubDate>Tue, 27 Nov 2007 10:52:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5350439</guid><dc:creator>Fredrik N</dc:creator><slash:comments>24</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=5350439</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2007/11/27/mvc-expression-language-and-custom-tags.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;FONT size=2&gt;I have seen several developers that want and have no problem by using server-side code blocks in a View when they are working with the MVC pattern. But I think the server-side code block should be used only in some advanced and special occasions and instead use an Expression Language. By using a lot of server-side code block can make the View messy and the solution can be difficult to implement, form the perspective of code maintainability and extensibility. If we instead use tags similar to HTML/XML we can make the code blend into HTML in a nice way. &lt;BR&gt;&lt;BR&gt;Take a look at the following code:&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;FONT color=#000000&gt;&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;if&lt;/FONT&gt; (ViewData != &lt;FONT color=#0000ff&gt;null&lt;/FONT&gt; &amp;amp;&amp;amp; ViewData.HasError) {&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;BR&gt;&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt; } &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt; &lt;FONT color=#0000ff&gt;if&lt;/FONT&gt; (ViewData.ExceptionType == &lt;FONT color=#0000ff&gt;ExceptionType.Critical&lt;/FONT&gt;) &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;div&lt;/FONT&gt; &lt;FONT color=#ff0000&gt;class&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;criticalException&lt;/FONT&gt;"&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Critical Error: &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt;= ViewData.ExceptionMessage&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt; &lt;FONT color=#0000ff&gt;if&lt;/FONT&gt; (ViewData.InnerException != &lt;FONT color=#0000ff&gt;null&lt;/FONT&gt;) } &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt;=ViewData.InnerException.Message&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt; } &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;div&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt; &lt;FONT color=#0000ff&gt;else&lt;/FONT&gt; &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;div&lt;/FONT&gt; &lt;FONT color=#ff0000&gt;class&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;exception&lt;/FONT&gt;"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Error message :&lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;br&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;/&amp;gt;&lt;/FONT&gt;&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt;= ViewData.ExceptionMessage&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;div&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt; } &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;The above code is only used to demonstrate how messy a View can be when we have a lot of server-side code blocks. This code will not be easy for a designer or web developer to maintain. If we instead use an Expression Language&amp;nbsp;(EL) and JSTL (JSP Stamdard Tag Library)&amp;nbsp;for JSP in ASP.Net the View can be written like this: &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;c&lt;/FONT&gt;:&lt;FONT color=#a52a2a&gt;if&lt;/FONT&gt; &lt;FONT color=#ff0000&gt;test&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;${ViewData!=null &amp;amp;&amp;amp; ViewData.HasError}&lt;/FONT&gt;"&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;c&lt;/FONT&gt;:&lt;FONT color=#a52a2a&gt;if&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;c&lt;/FONT&gt;:&lt;FONT color=#a52a2a&gt;choose&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;c&lt;/FONT&gt;:&lt;FONT color=#a52a2a&gt;when&lt;/FONT&gt; &lt;FONT color=#ff0000&gt;test&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;${ExceptionType == ExceptionType.Critical}&lt;/FONT&gt;"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;div&lt;/FONT&gt; &lt;FONT color=#ff0000&gt;class&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;criticalException&lt;/FONT&gt;"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Critical Error: &lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;c&lt;/FONT&gt;:&lt;FONT color=#a52a2a&gt;out&lt;/FONT&gt; &lt;FONT color=#ff0000&gt;value&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;${ViewData.ExceptionMessage}&lt;/FONT&gt;"&lt;FONT color=#0000ff&gt;/&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;c&lt;/FONT&gt;:&lt;FONT color=#a52a2a&gt;out&lt;/FONT&gt; &lt;FONT color=#ff0000&gt;value&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;${ViewData.InnerException.Message}&lt;/FONT&gt;"&lt;FONT color=#0000ff&gt;/&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;div&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;c&lt;/FONT&gt;:&lt;FONT color=#a52a2a&gt;otherwice&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;div&lt;/FONT&gt; &lt;FONT color=#ff0000&gt;class&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;exception&lt;/FONT&gt;"&lt;FONT color=#000080&gt;&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Error message :&lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;br&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;/&amp;gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;c&lt;/FONT&gt;:&lt;FONT color=#a52a2a&gt;out&lt;/FONT&gt; &lt;FONT color=#ff0000&gt;value&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;${ViewData.ExceptionMessage}&lt;/FONT&gt;"&lt;FONT color=#0000ff&gt;/&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/&lt;FONT color=#a52a2a&gt;div&lt;/FONT&gt;&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;c&lt;/FONT&gt;:&lt;FONT color=#a52a2a&gt;otherwice&lt;/FONT&gt;&amp;gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;c&lt;/FONT&gt;:&lt;FONT color=#a52a2a&gt;choose&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;The best part of the ${variable} is that it can evaluate null for us, and also do the type casting, so we don’t need to write:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt; &lt;FONT color=#0000ff&gt;if&lt;/FONT&gt; (ViewData.InnerException != &lt;FONT color=#0000ff&gt;null&lt;/FONT&gt;) } &lt;/FONT&gt;&lt;FONT size=2&gt;&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt;=ViewData.InnerException.Message&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt; } &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;or:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt;=ViewData.CustomerID.ToString()&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Instead we simply write:&lt;/FONT&gt;&lt;/P&gt;&lt;FONT face=Verdana size=2&gt;
&lt;P&gt;${ViewData.InnerException.Message}&lt;BR&gt;&lt;BR&gt;${ViewData.CustomerID}&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The best part, we shouldn't event need to use the ViewData, instead directly write:&lt;BR&gt;&lt;BR&gt;${CustomerID}&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Here is an example where a property path (sub-properties) is used:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;${Order.Rows[1].Item.Price}&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;In some cases we need some more logic to test some conditions etc, something that can’t be placed inside of a Controller. This logic can instead of being a server-code block be capsulated into an own custom tag.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;The problem with EL today is the lack of supports in tools, so we only get an error first at runt-time and some IDE don’t have support for easy handle the EL. To make it more powerful the tools need to handle it also. But I prefer this way instead of using server-side code block and I hope something similar will be added to the ASP.NET MVC Framework.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;A class="" href="http://www.nikhilk.net/Ajax-MVC.aspx" mce_href="http://www.nikhilk.net/Ajax-MVC.aspx"&gt;Nikhil wrote a post about using AJAX&lt;/A&gt; with the ASP.Net MVC Framework (only a prototype). He uses code like this:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt; RenderBeginAjaxForm(Url.Action(&lt;SPAN style="COLOR: #006080"&gt;"Add"&lt;/SPAN&gt;),&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; { Update=&lt;SPAN style="COLOR: #006080"&gt;"taskList, UpdateType="&lt;/SPAN&gt;appendBottom&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;SPAN style="COLOR: #006080"&gt;",&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Highlight="&lt;/SPAN&gt;True&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;SPAN style="COLOR: #006080"&gt;",&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Starting="&lt;/SPAN&gt;startAddTask&lt;SPAN style="COLOR: #006080"&gt;", Completed="&lt;/SPAN&gt;endAddTask" }); &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;input&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="text"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="name"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="nameTextBox"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;/&amp;gt;&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT size=2&gt;&lt;B&gt;&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt; Ajax.Watermark(&lt;SPAN style="COLOR: #006080"&gt;"nameTextBox"&lt;/SPAN&gt;,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; { watermarkText=&lt;SPAN style="COLOR: #006080"&gt;"[What do you need to do?]"&lt;/SPAN&gt;, watermarkCssClass=&lt;SPAN style="COLOR: #006080"&gt;"watermark"&lt;/SPAN&gt;}); &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;/B&gt;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;input&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="submit"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="addTask"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="Add Task"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;/&amp;gt;&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;&amp;lt;%&lt;/SPAN&gt; RenderEndForm(); &lt;SPAN style="BACKGROUND-COLOR: #ffff00"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;The RenderBeginAjaxForm will render a &amp;lt;form&amp;gt; element with an onClick and onSubmit attribute etc. The RenderEndForm will also add a &amp;lt;/form&amp;gt; The Ajax.Watermark is a way to extend a HTML element with a WaterMarker. I think this code is kind of “messy”. I would like to see some tags instead for example:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;ajax&lt;/FONT&gt;:&lt;FONT color=#a52a2a&gt;Form&lt;/FONT&gt; &lt;FONT color=#ff0000&gt;Action&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;Add&lt;/FONT&gt;" &lt;FONT color=#ff0000&gt;Update&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;takstList&lt;/FONT&gt;" &lt;FONT color=#ff0000&gt;UpdateType&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;appendBottom&lt;/FONT&gt;" &lt;FONT color=#ff0000&gt;Hightlight&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;true&lt;/FONT&gt;" ...&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;ajax&lt;/FONT&gt;:&lt;FONT color=#a52a2a&gt;waterMarkTextBox&lt;/FONT&gt; &lt;FONT color=#ff0000&gt;name&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;name&lt;/FONT&gt;" &lt;FONT color=#ff0000&gt;id&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;nameTextBox&lt;/FONT&gt;" &lt;FONT color=#ff0000&gt;markerText&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;[What do you need to do?]&lt;/FONT&gt;"...&lt;FONT color=#0000ff&gt;/&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;ajax&lt;/FONT&gt;:&lt;FONT color=#a52a2a&gt;form&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;By using this solution, tags will be nicely blended into HTML and remove server-side code and make it easy for web developers (They are used to HTML element and attribute) to simple apply it and also maintain it. We can also easy see that we have a &amp;lt;form&amp;gt; and a &amp;lt;/form&amp;gt;. This looks similar to Server controls today but this should be a more light-weight control, and the runat=”server” shouldn’t be used ;) This is similar to ViewComponent shipped with MonoRail (I think ;)), but I don’t like the declaration syntax MonoRail used. I prefer to use a HTML similar way to add components (custom tags) etc.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;What kind of solution do you prefer?&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5350439" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>ASP.NET MVC Framework - Ajax</title><link>http://weblogs.asp.net/fredriknormen/archive/2007/11/26/asp-net-mvc-framework-ajax.aspx</link><pubDate>Mon, 26 Nov 2007 16:00:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5342176</guid><dc:creator>Fredrik N</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=5342176</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2007/11/26/asp-net-mvc-framework-ajax.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;Take look at Nikhil's blog about how AJAX can be used with the ASP.Net MVC Framework:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;A href="http://www.nikhilk.net/Ajax-MVC.aspx"&gt;http://www.nikhilk.net/Ajax-MVC.aspx&lt;/A&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5342176" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/AXAJ/default.aspx">AXAJ</category></item><item><title>ASP.Net MVC Framework - Security</title><link>http://weblogs.asp.net/fredriknormen/archive/2007/11/25/asp-net-mvc-framework-security.aspx</link><pubDate>Sun, 25 Nov 2007 11:37:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5333017</guid><dc:creator>Fredrik N</dc:creator><slash:comments>16</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=5333017</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2007/11/25/asp-net-mvc-framework-security.aspx#comments</comments><description>&lt;P&gt;&lt;FONT size=2 face=Verdana&gt;&lt;STRONG&gt;&lt;EM&gt;Note: This post is based on an early version of the ASP.Net MVC Framework, not the final version, so changes my happen in the future.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2 face=Verdana&gt;When using the ASP.Net MVC Framework&amp;nbsp;it will&amp;nbsp;do a URL routing to route an URL to a Controller, and the Controller&amp;nbsp;should execute some logic and render the Views. We can for example use an URL like /Products/Edit/1 to edit a product. By using this kind of URL we need to care about security, because we don’t want everyone to be able to edit a product. How can we prevent some users to not edit the product? We can’t use the &amp;lt;location&amp;gt; element and deny certain user for accessing the View.&lt;BR&gt;&lt;BR&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;location&lt;/FONT&gt; &lt;FONT color=#ff0000&gt;path&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;Views/Products/Edit.aspx&lt;/FONT&gt;"&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;system.web&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;authorization&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;deny&lt;/FONT&gt; &lt;FONT color=#ff0000&gt;users&lt;/FONT&gt;="&lt;FONT color=#0000ff&gt;*&lt;/FONT&gt;"&lt;FONT color=#0000ff&gt;/&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;authorization&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;system.web&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#a52a2a&gt;location&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2 face=Verdana&gt;The above section will not work, well sort of; it will make sure no one can access the Edit.aspx page. But if we use the RenderView method to render a View the &amp;lt;location&amp;gt; element will not work, the View will still be rendered. So what other options do we have? When we use the MVC framework the Controllers are responsible to handle the logic and the render of Views. We can in this case add the security check into our Controllers, in this case into the Action methods. As you may know a Controller can have several actions, like Edit, Update and List etc and they can also render different Views. Maybe the List action should everyone be possible to call, but not the Edit or Update methods. So in this case we need to add the security check on the Edit and Update methods in a Controller. This can for example be done by using the Roles feature of ASP.Net:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2 face="Courier New"&gt;[&lt;FONT color=#0000ff&gt;ControllerAction&lt;/FONT&gt;]&lt;BR&gt;&lt;FONT color=#000080&gt;public void&lt;/FONT&gt; Edit(&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;? id)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&lt;FONT color=#000080&gt;if&lt;/FONT&gt; (!&lt;FONT color=#0000ff&gt;Roles&lt;/FONT&gt;.IsUserInRole(&lt;FONT color=#a52a2a&gt;"Admin"&lt;/FONT&gt;))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;throw new&lt;/FONT&gt; SecurityException(&lt;FONT color=#a52a2a&gt;"Access denied"&lt;/FONT&gt;);&lt;BR&gt;&amp;nbsp; ...&amp;nbsp;&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2 face=Verdana&gt;To avoid writing this check in the Action methods we can instead use the PrincipalPermissionAttributes shipped with .Net:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2 face="Courier New"&gt;[&lt;FONT color=#0000ff&gt;ControllerAction&lt;/FONT&gt;]&lt;BR&gt;[&lt;FONT color=#0000ff&gt;PrincipalPermission&lt;/FONT&gt;(SecurityAction.Demand, Role=&lt;FONT color=#a52a2a&gt;"Admin"&lt;/FONT&gt;]&lt;BR&gt;&lt;FONT color=#000080&gt;public void&lt;/FONT&gt; Edit(&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;? id)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp; ...&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2 face=Verdana&gt;If we want to make sure all Action methods in a Controller have the check, we can add the PrincipalPermissionAttribute to the Controller class:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2 face="Courier New"&gt;[&lt;FONT color=#0000ff&gt;PrincipalPermission&lt;/FONT&gt;(SecurityAction.Demand, Role=&lt;FONT color=#a52a2a&gt;"Admin"&lt;/FONT&gt;)]&lt;BR&gt;&lt;FONT color=#000080&gt;public class&lt;/FONT&gt; HomeController : &lt;FONT color=#0000ff&gt;Controller&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2 face=Verdana&gt;If we want to handle the SecurityException we can use the ExceptionHandlerAttribute I wrote about in my previous post. This can catch the SecurityException and Render a View that will display the exception message.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2 face="Courier New"&gt;[&lt;FONT color=#0000ff&gt;ControllerAction&lt;/FONT&gt;]&lt;BR&gt;[&lt;FONT color=#0000ff&gt;PrincipalPermission&lt;/FONT&gt;(SecurityAction.Demand, Role=&lt;FONT color=#a52a2a&gt;"Admin"&lt;/FONT&gt;]&lt;BR&gt;[&lt;FONT color=#0000ff&gt;ExceptionHandler&lt;/FONT&gt;(&lt;FONT color=#a52a2a&gt;"Error"&lt;/FONT&gt;, &lt;FONT color=#000080&gt;typeof&lt;/FONT&gt;(&lt;FONT color=#0000ff&gt;SecurityException&lt;/FONT&gt;))]&lt;BR&gt;&lt;FONT color=#000080&gt;public void&lt;/FONT&gt; Edit(&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;? id)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp; ...&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2 face=Verdana&gt;&lt;FONT face="Courier New"&gt;If we&lt;/FONT&gt; don’t want to use the PrincipalPermissionAttribute and instead write our own Security handler, we can override the OnPreAction method and implement the security check. The OnPreAction method will be executed before any Action methods are executed.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2 face="Courier New"&gt;&lt;FONT color=#000080&gt;protected override void&lt;/FONT&gt; OnPreAction(&lt;FONT color=#000080&gt;string&lt;/FONT&gt; actionName, System.Reflection.&lt;FONT color=#000080&gt;MethodInfo&lt;/FONT&gt; methodInfo)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;if&lt;/FONT&gt; (actionName == &lt;FONT color=#a52a2a&gt;"Edit"&lt;/FONT&gt;)&lt;BR&gt;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;if&lt;/FONT&gt; (!&lt;FONT color=#0000ff&gt;Roles&lt;/FONT&gt;.IsUserInRole(&lt;FONT color=#a52a2a&gt;"Admin"&lt;/FONT&gt;) || !User.Identity.IsAuthenticated || !User.Identity.Name = &lt;FONT color=#a52a2a&gt;"Administrator"&lt;/FONT&gt;)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;throw&lt;/FONT&gt; SecurityException(&lt;FONT color=#a52a2a&gt;"Access denied!"&lt;/FONT&gt;);&lt;BR&gt;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5333017" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>ASP.Net MVC Framework - Handling Exception by using an Attribute</title><link>http://weblogs.asp.net/fredriknormen/archive/2007/11/22/asp-net-mvc-framework-handling-exception-by-using-an-attribute.aspx</link><pubDate>Wed, 21 Nov 2007 23:04:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5307554</guid><dc:creator>Fredrik N</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=5307554</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2007/11/22/asp-net-mvc-framework-handling-exception-by-using-an-attribute.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;&lt;EM&gt;Note: This is based on the pre-CTP version of the ASP.Net MVC Framework and it is in an early stage.&lt;BR&gt;&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR&gt;I got a comment on one my previous post “&lt;A href="http://weblogs.asp.net/fredriknormen/archive/2007/11/19/asp-net-mvc-framework-exception-handling.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2007/11/19/asp-net-mvc-framework-exception-handling.aspx"&gt;ASP.Net MVC Framework - Exception Handling&lt;/A&gt;” that the Rescue style MonoRail is a nice solution to handle Exception thrown within a Controller. In this post I have added something similar to as the Rescue feature of MonoRail (at least I think so ;)) where an attribute can be added to a Controller or on an Action. The attribute can be used to specify which View that should be rendered when an exception is thrown. We can also specify different Views based on different exceptions. Something I also added to the code is to specify something I will call an ExceptionHanlder. The ExceptionHandler is a pre handler which will be executed before the specified exception View should be rendered. The idea of the ExceptionHandler is to log exception etc.&lt;BR&gt;&lt;BR&gt;By using an Attribute (ExceptionHandlerAttribute) on a Controller or an Action method, we don’t need to override the OnError method of the base Controller class. I made this solution simple by only creating my own custom Controller class (N2Controller) and override the OnError method of the Controller. In this method do my exception handling process. Before I show the code I will show you how we can use the ExceptionHandlerAttribute.&lt;BR&gt;&lt;BR&gt;The ExceptionHandlerAttribute has three argument, exceptionHandler, viewName, exceptionType. The exceptionHandler argument is where we pass the type of the pre ExceptionHandler we want to use when an Exception is thrown. The viewName is the name of the View to render the exception information (In this sample code the view specified must be located in the Views/Exceptions folder). The exceptionType takes a type of an Exception. If we only want to make sure the Exception Handler should only handle a specific exception thrown within the Controller or Action method, we use the exceptionType argument.&lt;BR&gt;&lt;BR&gt;Here is an example of different use of the ExcetpionHandlerAttribute, it can be used on both the Controller’s Class and Action methods:&lt;BR&gt;&lt;BR&gt;1. [ExceptionHandler(“Error”);&lt;BR&gt;2. [ExceptionHandler("Error", typeof(DivideByZeroException))]&lt;BR&gt;3. [ExceptionHandler(typeof(ExceptionHandler), "Error")]&lt;BR&gt;4. [ExceptionHandler(typeof(ExceptionHandler), "Error", typeof(DividedByZeroException))&lt;BR&gt;5. [ExceptionHandler(typeof(ExceptionHandler))&lt;BR&gt;6. [ExceptionHandler(typeof(ExceptionHandler), typeof(DividedByZeroException))&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;1 Will who the Error view when an exception is thrown.&lt;BR&gt;2 Will only show the Error View is the exception thrown is of type DividedByZeroException.&lt;BR&gt;3 Will Show the Error View and use a pre ExceptionHandler before the View is rendered.&lt;BR&gt;4 Will Show the Error View only if the DividedByZeroException is thrown, and use a pre ExceptionHandler before the View is rendered.&lt;BR&gt;5 Will use the ExceptionHandler when an exception is thrown.&lt;BR&gt;6 Will use the ExceptionHandler if the DividedByZeroException is thrown.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;To use ExceptionHandler we simply create a Class that implements the IExceptionHandler interface.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT color=#000080&gt;public interface&lt;/FONT&gt; IExceptionHandler&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;void&lt;/FONT&gt; PreExceptionHandling(&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt; actionName, &lt;FONT color=#0000ff&gt;Exception&lt;/FONT&gt; exception);&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;In the PreExceptionHandling method we could for example put logic to log an exception. The PreExceptionHandling method will be called before a View will be rendered (A View will not be rendered if it’s not specified).&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Here is an example how the ExceptionHandlerAttribute can be used:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;[&lt;FONT color=#0000ff&gt;ExceptionHandler&lt;/FONT&gt;(&lt;FONT color=#a52a2a&gt;"DefaultError"&lt;/FONT&gt;, &lt;FONT color=#000080&gt;typeof&lt;/FONT&gt;(&lt;FONT color=#0000ff&gt;DivideByZeroException&lt;/FONT&gt;))]&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;&lt;EM&gt;&amp;lt;- Will not be used&lt;BR&gt;&lt;/EM&gt;&lt;/STRONG&gt;&lt;FONT color=#000080&gt;public class&lt;/FONT&gt; HomeController : &lt;FONT color=#0000ff&gt;N2Controller&lt;/FONT&gt;&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#0000ff&gt;ControllerAction&lt;/FONT&gt;, &lt;FONT color=#0000ff&gt;ExceptionHandler&lt;/FONT&gt;(&lt;FONT color=#a52a2a&gt;"Error"&lt;/FONT&gt;)]&amp;nbsp; &lt;EM&gt;&lt;STRONG&gt;&amp;lt;- Will be used&lt;/STRONG&gt;&lt;/EM&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#000080&gt;public void&lt;/FONT&gt; Index()&lt;BR&gt;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;The attribute specified on the action method will have more to sat than the attribute on the Controller’s class.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;[&lt;FONT color=#0000ff&gt;ExceptionHandler&lt;/FONT&gt;(&lt;FONT color=#a52a2a&gt;"DefaultError"&lt;/FONT&gt;, &lt;FONT color=#000080&gt;typeof&lt;/FONT&gt;(&lt;FONT color=#0000ff&gt;DivideByZeroException&lt;/FONT&gt;))]&amp;nbsp;&amp;nbsp; &amp;lt;- Will be used&lt;BR&gt;&lt;FONT color=#000080&gt;public class&lt;/FONT&gt; HomeController : &lt;FONT color=#0000ff&gt;N2Controller&lt;/FONT&gt;&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#0000ff&gt;ControllerAction&lt;/FONT&gt;, &lt;FONT color=#0000ff&gt;ExceptionHandler&lt;/FONT&gt;(&lt;FONT color=#a52a2a&gt;"Error"&lt;/FONT&gt;,&lt;FONT color=#000080&gt;typeof&lt;/FONT&gt;(&lt;FONT color=#0000ff&gt;IndexOutOfRangeException&lt;/FONT&gt;),&lt;FONT color=#000080&gt;typeof&lt;/FONT&gt;(&lt;FONT color=#0000ff&gt;ArgumentException&lt;/FONT&gt;))]&amp;nbsp; &amp;lt;- Will be used&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;public void&lt;/FONT&gt; Index()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;In the code above, both Exception Handler will be used. For example, if the Action method throws a DidivedByZeroException the DefaultError View will be rendered. But if the IndexOurOfRangeException or ArgumetnException is thrown the Error View will be displayed.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Here is the implementation of my custom Controller:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT color=#000080&gt;public class&lt;/FONT&gt; N2Controller : &lt;FONT color=#0000ff&gt;Controller&lt;/FONT&gt;&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;protected override bool&lt;/FONT&gt; OnError(&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt; actionName, &lt;FONT color=#0000ff&gt;System.Reflection.MethodInfo&lt;/FONT&gt; methodInfo, &lt;FONT color=#0000ff&gt;Exception&lt;/FONT&gt; exception)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;ArrayList&lt;/FONT&gt; attributes = &lt;FONT color=#000080&gt;this&lt;/FONT&gt;.GetExceptionHandlerAttribute(methodInfo);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;foreach&lt;/FONT&gt; (&lt;FONT color=#0000ff&gt;ExceptionHandlerAttribute&lt;/FONT&gt; attribute in attributes)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;foreach&lt;/FONT&gt; (Type exceptionType in attribute.ExceptionTypes)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;if&lt;/FONT&gt; (exceptionType.IsAssignableFrom(exception.InnerException.GetType()))&lt;BR&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;FONT color=#000080&gt;if &lt;/FONT&gt;(attribute.ExceptionHandler != &lt;FONT color=#0000ff&gt;null&lt;/FONT&gt;)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ((&lt;FONT color=#0000ff&gt;IExceptionHandler&lt;/FONT&gt;)Activator.CreateInstance(attribute.ExceptionHandler)).PreExceptionHandling(actionName, exception);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;if&lt;/FONT&gt; (!&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt;.IsNullOrEmpty(attribute.View))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; RenderView(&lt;FONT color=#a52a2a&gt;"~/Views/Exceptions/"&lt;/FONT&gt; + attribute.View + &lt;FONT color=#a52a2a&gt;".aspx"&lt;/FONT&gt;, exception);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;return&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;false&lt;/FONT&gt;;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;return&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;true&lt;/FONT&gt;;&lt;BR&gt;&amp;nbsp;&amp;nbsp; }&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;private ArrayList&lt;/FONT&gt; GetExceptionHandlerAttribute(&lt;FONT color=#0000ff&gt;System.Reflection.MethodInfo&lt;/FONT&gt; methodInfo)&lt;BR&gt;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;ArrayList&lt;/FONT&gt; attributes = &lt;FONT color=#000080&gt;new&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;ArrayList&lt;/FONT&gt;();&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; attributes.AddRange(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; methodInfo.GetCustomAttributes(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;typeof&lt;/FONT&gt;(ExceptionHandlerAttribute),&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;false&lt;/FONT&gt;));&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; attributes.AddRange(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;this&lt;/FONT&gt;.GetType().GetCustomAttributes(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#000080&gt;typeof&lt;/FONT&gt;(ExceptionHandlerAttribute),&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#000080&gt;true&lt;/FONT&gt;));&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#000080&gt;return&lt;/FONT&gt; attributes;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;The code above is only on a prototype stage and. If no ExceptionHandlerAttribute is specified for an Action or Controller, the original ASP.Net MVC Frameowork’s OnError behavior will be used.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5307554" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>ASP.Net MVC Framework - Exception Handling</title><link>http://weblogs.asp.net/fredriknormen/archive/2007/11/19/asp-net-mvc-framework-exception-handling.aspx</link><pubDate>Mon, 19 Nov 2007 21:42:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5284986</guid><dc:creator>Fredrik N</dc:creator><slash:comments>29</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=5284986</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2007/11/19/asp-net-mvc-framework-exception-handling.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;&lt;EM&gt;Note: This post is based on an early preview version of the ASP.Net MVC Framework and much will happen until the next milestone.&lt;BR&gt;&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR&gt;In this post I will show how we can handle errors that occur in a Controller’s Action method.&lt;BR&gt;The Controller base class has a virtual method with the name OnError, this method takes three arguments, actionName, methodInfo and exception. It also has a return type of a Boolean. The idea when using the OnError method is to return true if we have handled the exception or false to letting ASP.Net handle it for us.&lt;BR&gt;&lt;BR&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#000080&gt;protected virtual bool&lt;/FONT&gt; OnError(&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt; actionName, &lt;FONT color=#0000ff&gt;System.Reflection.MethodInfo&lt;/FONT&gt; methodInfo, &lt;FONT color=#0000ff&gt;Exception&lt;/FONT&gt; exception)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;When an exception is thrown within our Action method, the OnError method will be executed. In the OnError method we can for example pass information to a View.&lt;BR&gt;&lt;BR&gt;Here is an example of a Controller where I will Render a View called “Error” and pass the exception to the View:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT color=#000080&gt;public class&lt;/FONT&gt; HomeController : &lt;FONT color=#0000ff&gt;Controller&lt;/FONT&gt;&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;protected override&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;bool&lt;/FONT&gt; OnError(&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt; actionName, &lt;FONT color=#0000ff&gt;System.Reflection.MethodInfo&lt;/FONT&gt; methodInfo, &lt;FONT color=#0000ff&gt;Exception&lt;/FONT&gt; exception)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RenderView(&lt;FONT color=#a52a2a&gt;"Error"&lt;/FONT&gt;,exception.InnerException);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;return&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;false&lt;/FONT&gt;;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#0000ff&gt;ControllerAction&lt;/FONT&gt;]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#000080&gt;public void&lt;/FONT&gt; Index()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; i = 0;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; sum = 10 / i;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RenderView(&lt;FONT color=#a52a2a&gt;"Index"&lt;/FONT&gt;);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;When the Index Action method will be executed a DevidedByZero Exception will be thrown, when this happens the OnError method will be called and it will render a view and return&amp;nbsp;false to specify that the error was handled.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;To make sure to have a common View for displaying the error message, we put it into the Shared subfolder of the Views folder:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;/Views&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /Shared&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Error.aspx&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;&lt;FONT face=Verdana size=2&gt;The Error View inherits the ViewData&amp;lt;Exception&amp;gt; class. To make this demo simple I just pass the Exception class to the View and show the value of the Message property:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Error.aspx.cs" Inherits="MvcApplication.Views.Error" %&amp;gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "&lt;/FONT&gt;&lt;A href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" mce_href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;&lt;FONT face="Courier New" size=2&gt;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New" size=2&gt;"&amp;gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;lt;html xmlns="&lt;/FONT&gt;&lt;A href="http://www.w3.org/1999/xhtml" mce_href="http://www.w3.org/1999/xhtml"&gt;&lt;FONT face="Courier New" size=2&gt;http://www.w3.org/1999/xhtml&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New" size=2&gt;" &amp;gt;&lt;BR&gt;&amp;lt;head runat="server"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;&lt;BR&gt;&amp;lt;/head&amp;gt;&lt;BR&gt;&amp;lt;body&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;div&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;h1&amp;gt;Error on page&amp;lt;/h1&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;b&amp;gt;&amp;lt;%=ViewData.Message %&amp;gt;&amp;lt;/b&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/div&amp;gt;&lt;BR&gt;&amp;lt;/body&amp;gt;&lt;BR&gt;&amp;lt;/html&amp;gt;&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5284986" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>ASP.Net MVC Framework - Creating a IRouteHandler which will support Interceptors</title><link>http://weblogs.asp.net/fredriknormen/archive/2007/11/19/asp-net-mvc-framework-creating-a-iroutehandler-which-will-support-interceptors.aspx</link><pubDate>Sun, 18 Nov 2007 23:42:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5274591</guid><dc:creator>Fredrik N</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=5274591</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2007/11/19/asp-net-mvc-framework-creating-a-iroutehandler-which-will-support-interceptors.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Verdana size=2&gt;I really love how extensible the ASP.Net MVC Framework is, for example the normal pipeline looks like this:&lt;BR&gt;&lt;BR&gt;Request -&amp;gt; Route (IRouteHandler) –&amp;gt; ControlFactory (IControllerFactory) -&amp;gt; Controller (IController) -&amp;gt; ViewFactory (IViewFactory) -&amp;gt; View (IView) -&amp;gt; Response&lt;BR&gt;&lt;BR&gt;By implementing my own IrouteHandler I changed the pipeline to support Interceptors before and after an action is invoked:&lt;BR&gt;&lt;BR&gt;Request -&amp;gt; Route (IRouteHandler) –&amp;gt; ControlFactory (IControllerFactory) -&amp;gt; (Pre) Interceptors (IInterceptor) -&amp;gt; Controller (IController) -&amp;gt; ViewFactory (IViewFactory) -&amp;gt; View (IView) -&amp;gt; (Post)Interceptors (Iinterceptor) -&amp;gt; Response&lt;BR&gt;&lt;BR&gt;On the Controller base class we have two methods, OnPreAction and OnPostAction, those methods will be executed before and after an Action method. But I wanted to be able to specify Interceptors for a Route which should be executed before a controller is instantiated and after a Controller’s action method is executed.&lt;BR&gt;&lt;BR&gt;The Interceptors has two methods, PreHandle and PostHandle. The PreHandle returns a value if the process of a request should be stopped or if it should go on with the process of an execution.&amp;nbsp; It’s possible to create a chain of Interceptors. If more than one Interceptor is specified and one of them tells to stop the process, the rest in the chain will not be executed. To specify an Interceptor or a chain of Interceptors I have override the Route class and created my own which has a List of IInterceptors. So when a Route is setup, Interceptors could be added to that route:&lt;BR&gt;&lt;BR&gt;&lt;FONT face="Courier New"&gt;RouteTable.Routes.Add(new Nsquared2.MVC.Extenstion.Route&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Url = "[controller]/[action]/[id]",&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Defaults = new { action = "Index", id = (string)null },&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;RouteHandler = typeof(N2MVCRouteHandler),&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Interceptors = new List&amp;lt;IInterceptors&amp;gt;()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new MyInterceptor(),&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new AnotherInterceptor()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;});&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Here is an example of an Interceptor which will redirect the user to a “Not available at this time” page if the current time of the Request is not within a specified interval.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;public bool PreHandle(RequestContext context)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (DateTime.Now.Hour &amp;gt; START_HOUR &amp;amp;&amp;amp; DateTime.Now.Hour &amp;lt; END_HOUR)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return true;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.HttpContext.Repsonse.Redirect(“NotAvailableAtThisTime.html”);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;return false;&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;The following is my implementation of the MvcHandler’s ProcessRequest to add the Interceptors to the pipeline; the code is also part of my own MvcHandler written in my previous post.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;protected override void ProcessRequest(IHttpContext httpContext)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp; if (this.RequestContext == null)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new InvalidOperationException("No RequestContext");&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp; if (base.RequestContext.RouteData.Route.GetType().IsAssignableFrom(typeof(Nsquared2.MVC.Exstension.Route)))&lt;BR&gt;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (InterceptorHandler.ExecutePreHandler(this.RequestContext))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ControllerContext controllerContext = this.ExecuteAction();&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; InterceptorHandler.ExecutePostHandler(controllerContext);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp; else&lt;BR&gt;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.ExecuteAction();&lt;BR&gt;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Here is the ExecutePreHandler method of my InterceptorHandler:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;public static bool ExecutePreHandler(RequestContext request)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Nsquared2.MVC.Extension .Route route = request.RouteData.Route as Nsquared2.MVC.Extension.Route;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (route.Interceptors != null)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (IInterceptors interceptor in route.Interceptors)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!interceptor.PreHandle(request))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;return true;&lt;BR&gt;&amp;nbsp;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;&lt;EM&gt;Note: This code is only used for a Interceptor prototype.&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5274591" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item></channel></rss>