MojoPortal allow custom modules to leverage MVC3

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
3/9/2011 5:20:28 PM
Gravatar
Total Posts 6

MojoPortal allow custom modules to leverage MVC3

I was wondering if it would be possible to allow custom modules to be written in MVC3.  I looked in the forums and ran across a few posts that basically state that mojoPortal is a webforms application and that to re-write to MVC would take too much.
(http://www.mojoportal.com/Forums/Thread.aspx?pageid=5&mid=34&ItemID=7&thread=4941&postid=20625 http://www.mojoportal.com/Forums/Thread.aspx?pageid=5&mid=34&ItemID=9&thread=3228&pagenumber=1
http://www.mojoportal.com/Forums/Thread.aspx?pageid=5&mid=34&ItemID=7&thread=7185&pagenumber=1
http://www.mojoportal.com/Forums/Thread.aspx?pageid=5&mid=34&ItemID=9&thread=5381&pagenumber=1)


I would agree that there is a lot of code in mojoPortal and that it would take a great effort to change to MVC.  I also understand there are other CMS options out there which leverage MVC.  With that being said I would like to write my custom modules for my clients to leverage MVC3, which wouldn’t cause mojoPortal to be re-written.


I made a few changes (4 files) to the source to allow custom modules to leverage MVC3.


/Web/mojoPortal.Web.csproj
Included a reference to System.Web.Mvc.dll (MVC3)
    <Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />

/Web/Components/Routing/RoutingHandler.cs
The GetRouteForElement method I changed the code as follows
        public static Route GetRouteForElement(RouteDefinition route)
        {
            IRouteHandler handler;
            RouteValueDictionary defaults = new RouteValueDictionary();
            RouteValueDictionary restrictions = new RouteValueDictionary();
            if (String.IsNullOrEmpty(route.VirtualPath))
            {
                if (route.RouteHandler == null)
                {
                    //Stop Route ...
                    handler = new StopRoutingHandler();
                }
                else
                {
                    handler = route.RouteHandler;
                }
            }
            else
            {
                handler = new mojoPortal.Web.Routing.RoutingHandler(route.VirtualPath);
            }
            foreach (RouteDefault d in route.RouteDefaults)
            {
                defaults.Add(d.ParameterName, d.DefaultValue);
            }

            foreach (RouteRestriction r in route.RouteRestrictions)
            {
                restrictions.Add(r.ParameterName, r.Restriction);
            }
            return new Route(route.RouteUrl,
                defaults, restrictions, handler);
        }
/Web/Components/Routing/RouteDefinition.cs
Added two using statements, a local variable, a public property and change the LoadRoutes method
using System;
using System.Collections.ObjectModel;
using System.Web;
using System.Web.Routing;
using System.Xml;
       private IRouteHandler routeHandler = null;
        public IRouteHandler RouteHandler
        {
            get { return routeHandler; }
        }
        public static void LoadRoutes(
            RoutingConfiguration config,
            XmlNode documentElement)
        {
            if (HttpContext.Current == null) return;
            if (documentElement.Name != "Routes") return;
            foreach (XmlNode node in documentElement.ChildNodes)
            {
                if (node.Name == "Route")
                {
                    RouteDefinition routeDef = new RouteDefinition();
                    XmlAttributeCollection attributeCollection
                        = node.Attributes;
                    if (attributeCollection["name"] != null)
                    {
                        routeDef.name = attributeCollection["name"].Value;
                    }
                    if (attributeCollection["routeUrl"] != null)
                    {
                        routeDef.routeUrl = attributeCollection["routeUrl"].Value;
                    }
                    if (attributeCollection["virtualPath"] != null)
                    {
                        routeDef.virtualPath = attributeCollection["virtualPath"].Value;
                    }
                    if (attributeCollection["routeHandler"] != null &&typeof(IRouteHandler).IsAssignableFrom(Type.GetType(attributeCollection["routeHandler"].Value)))
                    {
                        routeDef.routeHandler = Activator.CreateInstance(Type.GetType(attributeCollection["routeHandler"].Value)) asIRouteHandler;
                    }
                  
                    foreach (XmlNode child in node.ChildNodes)
                    {
                        if (child.Name == "Defaults")
                        {
                            RouteDefault.Load(
                                routeDef,
                                child);
                        }
                        if (child.Name == "Restrictions")
                        {
                            RouteRestriction.Load(
                                routeDef,
                                child);
                        }
                    }
                    config.RouteDefinitions.Add(routeDef);
                }
            }
        }

/Web/Web.config
Added namespaces to the pages element
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages"/>

Uncomment modules setting
    <modules runAllManagedModulesForAllRequests="true" />

Added runtime setting for MVC
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

 

What would the possibility be of getting this source integrated into the main source?

3/10/2011 12:43:16 AM
Gravatar
Total Posts 245
mojoPortal Community Expert

Re: MojoPortal allow custom modules to leverage MVC3

Interesting thought.

5 months ago Scott Hanselman blogged about mixing up:

    * ASP.NET MVC
    * ASP.NET WebForms
    * ASP.NET Web Services (ASMX)
    * WCF Services
    * ASP.NET Dynamic Data
    * ASP.NET AJAX

and creating a Hybrid app.  The blog can be viewed here.

The Blog says "You can (and should) feel free to have Hybrid applications".

Michael.  If you follow through on this would you please document and write a Blog post on How to...?
I know I'm not the only one who would appreciate it.

Good Luck!

3/10/2011 2:27:48 PM
Gravatar
Total Posts 245
mojoPortal Community Expert

Re: MojoPortal allow custom modules to leverage MVC3

MMMMmmmm..........

This is also interesting!!!

A few minutes ago this was added to the source code.

"changes and added files to support use of mvc3 in custom features "

That does not mean Joe is finished, but it's looking promising.

Rick

3/10/2011 7:54:51 PM
Gravatar
Total Posts 6

Re: MojoPortal allow custom modules to leverage MVC3

Hey Rick,

Looks to me that Joe has implemented the changes.  I have tested and the changes are a go!

Thanks,

Michael

3/11/2011 6:15:59 AM
Gravatar
Total Posts 18439

Re: MojoPortal allow custom modules to leverage MVC3

Hi Guys,

I implemented the changes except for

Uncomment modules setting
    <modules runAllManagedModulesForAllRequests="true" />

For now you will have to edit the web.config yourself to enable that.

My only other concern is whether that Activator.CreateInstance() might cause errors under medium trust. We need to test for that and if needed wrap error handling around it.

Michael's changes are changes to a plugin system I implemented to allow the possibility to plugin routes and routing which can be used with or without MVC. Currently this is not really use din any mojoportal features but it is plumbing for things I want to do in the future.

I'd like to re-iterate that mojoPortal is never changing to MVC.

These changes will allow you the possibility to use MVC for bolt on features or in supporting pages of CMS features. The FeatureModule itself I think would still need to be a UserControl. And using MVC for supporting pages of CMS features will pose challenges for visual integration since it would not use the skinning system. So I'm not sure MVC is feasible for use in CMS plugin features, but it could be used for bolt on features. Maybe you guys will prove me wrong.

What I mean by "bolt on" features is features that are not CMS plugins. For example my add on product In Site Analytics Pro is mainly a bolt on feature (though it does include 2 small cms plugins), it has a completely different ui than the main site and you access it from a link in the administration menu. This feature could have been implemented with MVC, I considered it but for expedience I went with WebForms.  Bolt On features are the kind of thing you could build as a standalone app but by bolting it into mojoPortal you can at least still take advantage of a lot of mojo features such as roles and permissions and reduce overall development effort.  

Best,

Joe

 

3/17/2011 8:04:43 PM
Gravatar
Total Posts 6

Re: MojoPortal allow custom modules to leverage MVC3

I have been able to apply the layout.Master to the MVC3 "Bolt on" application.  Once I finish this for my client I plan on putting a post together about how to accomplish this feat.

Joe - Should I create a blog post on my website and provide a link in this forum thread or how does developers normally accomplish this type of technical post?

3/18/2011 5:00:50 AM
Gravatar
Total Posts 18439

Re: MojoPortal allow custom modules to leverage MVC3

Hi Michael,

I leave that up to you, but if it works well we would love to have an article in the documentation on this site. I could setup a page for you with edit permissions.

Best,

Joe

10/25/2011 10:53:46 AM
Gravatar
Total Posts 2

Re: MojoPortal allow custom modules to leverage MVC3

Hey Joe,

I know you have years of effort into developing MojoPortal. Do you still stand by your comment that MojoPortal will never be changing to MVC?  I was just wondering why not move to using MVC over your current architecture over time.  Is this for backwards compatibility reasons or do you not think MVC is the way to go? I am just getting into MVC 3 so I am just looking for some feedback here.

Ryan

10/25/2011 11:06:52 AM
Gravatar
Total Posts 18439

Re: MojoPortal allow custom modules to leverage MVC3

MVC is not compatible with WebForms, you can use MVC for bolt on features but not for features that integrate with existing pages and features. Nothing from webforms can be re-used in MVC, the skinning model cannot be shared so no easy visual integration between them. See this thread for further recent comments. Changing to MVC would break compatibility with all previous versions, all add-on products and custom features that others have built on top of mojoPortal in addition to requiring re-implementing 7 years of work.

Not likely to happen unless someone wants to bankroll me for the next few years, it is not feasible from a business point of view, development is not free. MVC has its merits, but WebForms is still alive and well and is more approachable for average developers and more suited to rapid development.

Joe

You must sign in to post in the forums. This thread is closed to new posts.