Last week, Shaun posted a blog in which he discussed whether DotNetNuke (DNN) should be rewritten in ASP.NET MVC.  This blog created quite a stir, both within the DotNetNuke Community and within the ASP.NET Community as a whole. 

In a comment that I added to Shaun’s post I suggested that the debate should not be over whether DNN should be rewritten in ASP.NET MVC (or any other framework that may come in the future), the discussion should be on “How can we enable developers to use the ASP.NET Technologies of their choice when developing extensions for DNN.”  After all, if we rewrite DNN in MVC we effectively lock out WebForm developers.

Thanks to an idea from my son Andrew (much of what I used in this work was from his Maverick project on codeplex) I have been able the create a Framework that sits on top of DotNetNuke (and System.Web.MVC) and allows modules to be created using the ASP.NET MVC Framework (DotNetNuke.Web.Mvc).

In this article I will review how – using this add-on layer we can go about creating a simple ASP.NET MVC module.  First, add an ASP.NET MVC Application to your Visual Studio solution. (See Figure 1 below)

Figure 1 – Add a new MVC Web Application in the Desktop Modules folder called MVC_Test
MVCModule01

Note that the location of the project is in the DesktopModules folder of our test website.  This is exactly the same process we would use to add a WAP (Web Application Project) style module. Figure 2 below shows the Solution Explorer after adding this project.

Figure 2 – The new Project in Solution Explorer
MVCModule02

Before we go any further – we should prove to ourselves that this is a valid ASP.NET MVC Web Application, by selecting the Default.aspx file, and selecting View in Browser from the Context menu (see Figure 3 below)

Figure 3 – Browsing to the site demonstrates that this is a valid ASP.NET MVC Application
MVCModule03

So now we have an MVC Application, how do we make it run as a Module in our DotNetNuke website?  First we need to add a new class to our project – MVC_TestApplication.cs (see Figure 4).

Figure 4 – Add a new class MVC_TestApplication to the root of the Application
MVCModule04

And we need to add references to our DotNetNuke Library project and to the new DotNetNuke.Web.Mvc project (see Figure 5)

Figure 5 – Add references to the DotNetNuke Library and the new DotNetNuke.Web.Mvc project
MVCModule05

When we have done that we need to open the new class we added and add some very simple code.

Listing 1 – The MVc_TestApplication class
   1:  using System.Web.Routing;
   2:  using DotNetNuke.Web.Mvc;
   3:  using DotNetNuke.Web.Mvc.Routing;
   4:   
   5:  namespace MVC_Test
   6:  {
   7:      public class MVC_TestApplication : MvcModuleApplication 
   8:      {
   9:          protected override string FolderPath 
  10:          {
  11:              get { return "MVC_Test"; }
  12:          }
  13:   
  14:          protected override void Init() 
  15:          {
  16:              base.Init();
  17:              RegisterRoutes(Routes);
  18:          }
  19:   
  20:          private static void RegisterRoutes(RouteCollection routes) 
  21:          {
  22:              routes.RegisterDefaultRoute("MVC_Test.Controllers");
  23:          }
  24:      }
  25:  }

The most important thing to note is that this class inherits from MvcModuleApplication, a new base class in the new DotNetNuke.Web.Mvc project.  The Init method allows us to register any routes that our MVC Module Application will need and the FolderPath property tells the DotNetNuke.Web.Mvc project where our Views are located.  Finally we need to make a very small change to the default View (Listing 2).

Listing 2 – Index.aspx
   1:  <%@ Page Language="C#" MasterPageFile="../Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage" %>
   2:   
   3:  <asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
   4:      Home Page
   5:  </asp:Content>
   6:   
   7:  <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
   8:      <h2><%= Html.Encode(ViewData["Message"]) %></h2>
   9:      <p>
  10:          To learn more about ASP.NET MVC visit <a href=http://asp.net/mvc 
                               title="ASP.NET MVC Website">http://asp.net/mvc</a>.
  11:      </p>
  12:  </asp:Content>

If you blink you won’t see the change. 

The change is to the reference to the MasterPageFile – the value of the attribute is changed from “~/Views/Shared/Site.Master” to “../Views/Shared/Site.Master”.  The reference points to the same file – but the original reference assumes that the Views folder is at the root of the IIS Application – as a module it is actually at ~/DesktopModules/MVC_Test/Views”.  By making the reference relative it will work in both scenarios.

Next we need to create our Module Extension.  This is done in much the same way as we do today, the only difference being that when we register the Module Control we use the Namespace for the new class that we added – MVC_Test.MVC_TestApplication (See Figure 6 below).  Since version 5.0, DNN has allowed module controls to be server controls and ultimately our new MVC_TestApplication class inherits from Control.

Figure 6 – Add the MVC_TestApplication as the Source for the Default Module Control
MVCModule06

Finally build our new MVC Module Application and copy the assembly from the bin folder of the project to the bin folder of the Website.

We are now ready to see if everything works.  Add a new Page and add the newly registered Module to the Page and “hey presto” we get a Module that looks like the MVC Application (See Figure 7 below)

Figure 7 – Add the Module to the Page
MVCModule07

The really cool thing is that we can still browse to the site (as an MVC Application) by selecting the Default.aspx page, and selecting View in Browser from the context menu, as we did in Figure 3 above.

Conclusions

In summary I have demonstrated in this article, that using ASP.NET MVC for Module Development is a possibility.  There still are a number of issues to resolve however, including, but not limited to:

  1. Handling routes other than the default route.
  2. Do the Html Helpers in ASP.NET MVC still work? and if not how can we make them work?
  3. As Webforms allows only one Form tag which is defined in the base page -Default.aspx -how do we handle Forms in an MVC Module.
  4. Issues around the use of Master pages and styles, and DNN skinning – we can solve this by removing the dependency on Master pages which is not a requirement of ASP.NET MVC.

I don’t expect any of these to be show-stoppers, but much more work still needs to be done.  In future blogs, I will describe how what I describe in this article was done and report on my attempts to resolve the remaining issues.


Posted in: DotNetNuke  Tags: , , ,

Comments


January 28. 2010 08:28
Mitchel Sellers
Charles,

This is awesome!  I can't wait to get a bit of time to dive into this a bit myself.


January 28. 2010 08:48
trackback
DotNetNuke & MVC

Earlier this week, Shaun Walker, DotNetNuke 's Co-Founder and Chief Architect, caused a bit of a ruckus


January 28. 2010 08:49
Brian Dukes
Very exciting work, Charles.  Keep it up!


January 28. 2010 08:52
Vitaly Kozadayev
Just wanted to drop a quick THANK YOU, CHARLES - YOU ARE MY HERO!!!!!!!!!!!!!

And I totally agree with your intro statement - in context of DNN, the discussion should be "How do we implement MVC", not the analysis of either platform's eligibility.


January 28. 2010 08:54
Vitaly Kozadayev
Sorry - just a clarification to the previous - "How do we implement MVC" as in "include" it in DNN, not rewrite DNN...


January 28. 2010 09:01
Vitaly Kozadayev
What happened to my comments Smile - was it something I said?


January 28. 2010 09:27
cnurse
@Vitaly - At the moment you can't - as the code I used has not been released, but I hope to get that done over the weekend or next week,  It does NOT require a change to core DNN code - and while I developed it against DNN 5.2 it should work with any DNN 5 release.

@Vitaly - comments are moderated and I was not at my PC!!


January 28. 2010 18:55
trackback
Social comments and analytics for this post

This post was mentioned on Twitter by cnurse: Blogged:  Developing Modules for DotNetNuke using ASP.NET MVC - Part 1, Introduction  -  http://bit.ly/aRV6V9 #dnn, #aspnet, #mvc


United States kevin southworth
January 28. 2010 21:10
kevin southworth
This is a really great project/undertaking, I would love to be able to create ASP MVC projects that can be run as DNN modules!  Will definitely be looking forward to your future posts on this!
no site


January 29. 2010 08:08
Michael Fayez
WOW I was really thinking of this and was searching the web a week ago to find an article that speaks about this topic but there was none till you wrote this post Smile
Yes I am not interested of totally rewriting DNN to use MVC instead of Web Form. I am interested of having the liberty to write DNN custom modules in any ASP.Net framework according to requirements.
I also tried to find an article or a post that would speak about developing custom modules using ASP.Net Dynamic Data. Do you have something for us about this? Smile


January 30. 2010 21:40
cnurse
@Micahel Fayez

I am afraid that Dynamic Data is a harder nit to crack - so far we haven't found a way to use it.


January 31. 2010 18:44
Michael Washington
Charles, just wanted to drop a note and say "of course this is a GREAT thing you are doing!" Smile This will make me start using MVC. Can't wait for you to finish so I can do my "DotNetNuke MVC module for absolute beginners"...

on Dynamic Data. i did some work here:
www.adefwebserver.com/.../...NN_DynamicDatTest.htm

The issue is that Dynamic Data wants to hook into the application startup (Global.asax) to work properly. So if the Core put in the hooks...


February 7. 2010 18:14
rocreh web design
thanks for a lot of information i will try this features.


February 17. 2010 14:15
Moises Kringel
This is a excellent post, but I was wondering how do I suscribe to the RSS feed?


If We All Quit Voting Will They All Go Away?
"An evil exists that threatens every man, woman, and child of this great nation. We must take steps to ensure our domestic security and protect our Homeland." - Adolph Hitler


February 26. 2010 16:26
Carlos Adolfo Ortiz Q
Good article, I am studying ASP.NET MVC 1.0 and it showed a little attention to try this as I am using DNN as well, but to my surprise, you mention DotNetNuke.Web.Mvc, and DotNetNuke.Library; where are those resources found? I don't get it.


February 26. 2010 16:33
Carlos Adolfo Ortiz Q
Well. I am studying ASP.NET MVC, but also use DNN. And this Showed very valuable. But one thing. Where do I find DotNetNuke.Web.Mvc and DotNetNuke.Library resources.


February 26. 2010 16:37
Carlos Adolfo Ortiz Q
Where to find DotNetNuke Library and the new DotNetNuke.Web.Mvc project


February 26. 2010 22:01
cnurse
The DotNetNuke.Library project is part of the core package.  The new DotNetNuke.Web.Mvc project is in a codeplex project dnnlabs.codeplex.com


March 2. 2010 09:38
trackback
Developing Modules for DotNetNuke using ASP.NET MVC – Part 1, Introduction

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

Comments are closed

 Search Blog

 Adsense

 Calendar

«  March 2010  »
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234
View posts in large calendar
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 Thoughts from the Wet Coast