Posts Tagged ‘MVC’

July 27th, 2010

ASP.NET MVC 3 Preview 1 Released

The title says it all, so go get your copy today and check out all the new features.  You can download it here.

New features include:

  • Razor View Engine
  • Dynamic View and ViewModel Properties
  • “Add View” Dialog Box Supports Multiple View Engines
  • Service Location and Dependency Injection Support
  • Global Filters
  • New JsonValueProviderFactory Class
  • Support for .NET Framework 4 Validation Attributes and IValidatableObject
  • New IClientValidatable Interface
  • Support for >NET Framework 4 Metadata Attributes
  • New IMetadataAware Interface
  • New Action Result Types (HttpNotFoundResultAction and HttpStatusCodeResultAction)
  • Permanent Redirect Support in the controller (RedirectPermanent, RedirectToRoutePermanent, and RedirectToActionPermanent)

Looks like this is going to be a very worth while upgrade, and a special thanks should be given to Phil and team for making ASP.NET MVC everything that ASP.NET WebForms isn’t.

Tags: , ,

Posted in Programming | kick it on DotNetKicks.com | Bookmark | View blog reactions | No Comments »

May 11th, 2009

Creating Your First MVC ViewEngine

A question that I have been hearing a lot lately is:

How do I change the view location in MVC?

But what they really mean to say is:

How do I create a new ViewEngine that uses the view locations of my choosing?

It is actually very simple to do, and once you see it, I think you will agree with my assessment.  The first thing we are going to do to create our custom ViewEngine, is define the paths that we want to use for our master pages, view pages, and shared pages.  I have taken the liberty to define the following paths, you can customize them however you wish:

  • Master Pages:
    ~/Templates
    it use to be ~/Views/Shared or the controllers view
  • View Pages:
    ~/Views
  • Shared Pages:
    ~/Common
    it use to be ~/Views/Shared

The next thing we need to do is create a new class for our ViewEngine, for this example we are going to call it SimpleViewEngine.

public class SimpleViewEngine : VirtualPathProviderViewEngine
{
}

As you might have noticed from above our SimpleViewEngine inherits from VirtualPathProviderViewEngine, this is the root ViewEngine that uses the VirtualPathProvider (VPP). The VPP provides a way for web applications to read files off the file system in their local web application, so it is perfect for what we are doing. If you don’t want a file system based ViewEngine, and maybe want a ViewEngine based from the database, you can use the IViewEngine interface to create your own custom ViewEngine that fits your needs. (MVC is very flexible, by design)

The next thing we need to do is code our paths in to our SimpleViewEngine. We will do this in the constructor, so that they only have to be initialized once for the entire life span of our SimpleViewEngine.

public SimpleViewEngine ()
{
	/* {0} = view name or master page name
	 * {1} = controller name
	 */

	// create our master page location
	MasterLocationFormats = new[] {
		"~/Templates/{0}.master"
	};

	// create our views and common shared locations
	ViewLocationFormats = new[] {
		"~/Views/{1}/{0}.aspx",
		"~/Common/{0}.aspx",
	};

	// create our partial views and common shared locations
	PartialViewLocationFormats = new[] {
		"~/Views/{1}/{0}.ascx",
		"~/Common/{0}.ascx"
	};
}

As you can see the format is pretty straight forward. We create a string[] array with the paths of where our master pages, views, and common views are located. The only thing that we need to do is set place holders in our path so the the VirtualPathProviderViewEngine can replace the master name, view name, and controller name to construct our appropriate path.

  • {0}: is the view name or master page name.
  • {1}: is the controller name.

After we have done the hard part, which honestly wasn’t that hard, of creating the constructor with the paths, we just need to return the view objects from the constructed partial paths. Since we are using the standard ASP.NET Web Form (ASPX/ASCX) rendering engine. We are able to leverage the work already done by the MVC team and just return a new instance of the WebFormView object.

protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
	return new WebFormView(partialPath, null);
}

protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
	return new WebFormView(viewPath, masterPath);
}

Nothing really earth shattering here, just simply filling out the constructor with the proper parameters from our method, and then returning the newly created view. If you wanted to create a view based out of the database, or off your own syntax (meaning not ASP.NET syntax) then you would have to create your own view based off of the IView interface. But for this example we are only concerned with changing where our views are located.

There is one more thing that we need to do, and that is register our new SimpleViewEngine for use in the framework. The registration of view engines is done in the Global.asax, similar to the same way we register new routes.

public static void RegisterViewEngines(ViewEngineCollection viewEngines)
{
	viewEngines.Clear();
	viewEngines.Add(new SimpleViewEngine());
}

public static void RegisterRoutes(RouteCollection routes) { ... }

protected void Application_Start()
{
	RegisterRoutes(RouteTable.Routes);
	RegisterViewEngines(ViewEngines.Engines);
}

So we are now done. You have created a new view engines, defined your own routes, and registered this view engine with the MVC framework. Some other types of paths you may want to consider trying for your applications, using a custom ViewEngine, are special folders for your mobile or Facebook versions of your website.

  • Mobile: ~/Views/{1}/Mobile/{0}.aspx
  • Facebook: ~/Views/{1}/Facebook/{0}.aspx

I told you it was simple and straight forward, and I hope you agree that the MVC team has done an awesome job at providing a very flexible framework for us to tweak and customize it so it fits our applications.

Tags: , , , ,

Posted in How To | kick it on DotNetKicks.com | Bookmark | View blog reactions | 10 Comments »

October 28th, 2008

ASP.NET MVC Has Changed My Life

Lately I have been neglecting my blog and not posting as often as I would have liked.  I have had some very exciting things start up, at the begining of the summer, in my life that 6 months ago I would have said “no way is that going to happen.”  These new adventures are the growing popularity of my blog, public speaking engagements, and a new book I was asked to write on ASP.NET MVC. Many of you picked up on the fact that I was writing a book based on my profile on the Philly.NET website, but I had officially announced it until this post.  Many of my friends and family don’t even known I am writing a book so feel lucky in knowning you guys are some of the first people to know.

As you can imagine writing a book is about the same as writing a detailed blog post only 10,000% harder, because a two page post now needs to be extended out in to a 600 page book that needs to flow just as easily as the 2 page post did.  Luckily for me my best friend, Al Katawazi who I have known since 9th grade, is also a pretty decent programmer from the Rochester, NY area and he agreed to help me get this book out the door by the end of the year to align with the ASP.NET MVC release.

The book is titled ASP.NET MVC Website Programming: Problem – Design – Solution, and can currently be found at the following places for pre-order:

Sorry about the lack of a book picture, I haven’t had the time to go out and get a professional photo taken of me for the cover.  That will be coming pretty soon.

The current description of the book according to the Wiley site is, which also pretty well sums up the book as a whole in my opinion:

The utility of this book will be a nuts and bolts how-to guide on creating a website using MVC.  It will solve some of the most common problems that programmers run into when creating their first application or when trying to upgrade a current application to this new technology.

The book, much like the first one, will break each section down into 3 parts: the Problem, the Design, and the Solution.  For the most part, the chapter outline will be mostly the same, because the majority of the chapters are still just as important now to web developers as they were when the book was originally written. However, this edition includes MVC updates within these chapters.

Some of the site features covered in the book and provided in the TheBeerHouse example/framework code are:

  • registration and membership system and user-selectable themes
  • content management system for articles and photos
  • polls, mailing lists, and forums
  • e-commerce store, shopping cart, order management with real-time credit-card processing
  • localization

In building and working with these features the site developer will learn:

  • Master pages, themes, membership, and profiles
  • Server-side UI controls
  • Compilation, deployment, instrumentation, error handling and logging
  • Data access
  • The MVC (model view controller) approach to separation of the site UI and presentation layer from the pluggable data access layer and business logic layer

The code for the book will be published to CodePlex probably this weekend, that is if you want a sneak peak at what has taken me away from posting on my blog for the past couple of months.

http://www.codeplex.com/TheBeerHouse

If you want to wait for the book I totally understand, because who wants to spoil Christmas by peaking at the presents.  I am just kidding, I don’t think my code can be compared to Christmas morning, but maybe the 3rd Tuesday morning of October.

All kidding aside I really have to thank Scott Guthrie, Phil Haack and the whole ASP.NET MVC, because with out the great support for this product and the fore sight of Microsoft to actually create a product that ALT.NET developers like my self have been crying out for. I wouldn’t be in this place in my life.  I never thought that an interest in a small alpha release of a product back in December 2007 would lead me to writing a book, but it has been a great ride and I only hope it continues.

I am going to continue to try and post content that is useful to other developers like myself as much as I can, but please bear with me until the end of the year when all my chapters will be written and the book will be getting ready for the presses.

Tags: , , , ,

Posted in News, Personal | kick it on DotNetKicks.com | Bookmark | View blog reactions | 8 Comments »