13.12. Extending MVC

ASP.NET MVC is very easy to customize and extend. You don't like how the view engine works? No problem—you can override it. ASP.NET MVC is a framework practically begging for customization.

13.12.1. Extension Methods

One of the easiest ways to extend the existing ASP.NET MVC functionality is by writing extension methods on some of the HtmlHelper classes. For example, you might need to render a home page link to a specific page throughout your application. To accomplish this, you can create an extension method.

Let's do so now. Create a new class called UrlHelperExtension.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Chapter13.BobsMoviesMVC.Controllers
{
    public static class UrlHelperExtension
    {
        public static string AllFilms(this UrlHelper helper)
        {
            return helper.Content("~/Film/All");
        }
    }

}

You can then use this as follows:

<a href="<%= Url.AllFilms()  %>">All Films</a>

13.12.2. Filters

ASP.NET MVC allows the creation of reusable blocks of code called filters that intercept and process various types of requests. Common uses of filters include authorization and logging. For information on overriding controller classes and views, see the "Further Reading" section at the end of the chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.126.56