Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Funka! 398 posts 661 karma points
    Jun 07, 2013 @ 00:14
    Funka!
    0

    How are people developing MVC apps within Umbraco v6?

    Hi there,

    I'm familiar with building MVC apps, and very familiar with building Umbraco 4 sites, but trying to figure out how best to build an MVC app into an Umbraco v6 site right now is eluding me.

    I have Visual Studio 2010 and downloaded the (compiled) Umbraco version 6.1.1 today from Codeplex. I setup an IIS website and SQL database and went through the normal installation process. I read in another thread that in VS I should open this as a "web site" and not a project, and that I should set the F5 start action to "No Build" and to not build the website as part of a solution. (There was also advice in that thread how to debug by attaching to my IIS process, but I haven't made it that far yet.)

    So my question then is, where are people writing their controllers and storing their models? In a separate project that is compiled into a DLL that I can shove into my new site's bin folder? (If so I would hope there is some automated way I can do that whenever I hit F5.) Or is the recommendation to put these into the App_Code folder? Are there any extra changes I need to make to the umbraco website? Or, maybe I'm doing this all wrong and should start with an MVC web project and then import the Umbraco binaries? Also, where do I define my routes? (Global.asax refers to Global.asax.cs, which doesn't exist in my pre-compiled site.) Do I need to download full source code of Umbraco to do what I'm asking?

    So as you can in my last paragraph, I have a lot of questions and am really struggling with finding any good "how-to" guides that can lay this out for me in a simple manner from start to finish. As I said, I know how to build an MVC app but just not sure how to do this in Umbraco 6. The documenation section does have some helpful stuff in there, but doesn't really say how to start or how to structure things within Visual Studio. I used to think I was pretty clever but am feeling really dumb right now.

    Thank you!

  • Funka! 398 posts 661 karma points
    Jun 07, 2013 @ 19:54
    Funka!
    0

    I downloaded and installed the Standard Website MVC starter kit package and have been looking at how this was setup. So to answer my own question, putting your controllers in the App_Code folder seems to be one way to do this. For all of the documentation I've read, this one point seems to have been lost on me, but maybe should have been obvious (as I had theorized about, above). Although, having a single solution in Visual Studio where I could maintain my model and data layers and seamlessly develop/debug seems more ideal, so I will still work on figuring out how to get Visual Studio to do this. (Perhaps running a second instance of VS with my desired solution, then setup some build events to copy my DLLs from the solution project to the website's Bin folder every time?) In any case, I'm glad I found this package to study and learn from but do still have a ways to go.

    Thanks again for reading or for any advice.

  • Peter Laurie 8 posts 27 karma points
    Jun 08, 2013 @ 12:21
    Peter Laurie
    0

    I have ben struggling badly with this as well. The obnly place that I can get my surface controller to work is when I place it in the App_Code folder as well, and include the model in the same controller file in App_Code. Nothing works if I create a controller and models folders and place them in there.

    I have also struggled on trying to overide the application start with the same issues, there is no global.asmx.cs file, where to I place my application start files, in App_Code as well?

    Plesase can someone shed some light on this.

    I have installed the scaffolder and it places files in controller/custoncontrollers and controllers/surfacecontollers, then these do not work either.

    Thank you in advance for any advice as well.

  • kim Thomsen 59 posts 277 karma points
    Jun 09, 2013 @ 10:24
    kim Thomsen
    1

    You should install umbraco via nuget, then the hole umbraco solutions is included. 
    I only create Masterpages in umbraco everything ells i create in Visual studio.

    If i run into a problem i can most of the time find the solution here:

    http://our.umbraco.org/Documentation/Reference/Mvc/

  • Funka! 398 posts 661 karma points
    Jun 11, 2013 @ 22:44
    Funka!
    0

    @Peter,

    My guess is that since the recommendation is to open Umbraco as a "web site" rather than a project, you won't be able to use the traditional /Controllers and /Models folders since these won't be compiled (there is no "build process" when doing it this way), and hence you would either need to compile these within another project and move the DLL files into the website's /bin folder yourself, ... or else keep everything within the magical App_Code folder which will automatically be compiled for you. I am not sure I know what the "scaffolder" is, but think I perhaps read somethign about this being a VS plugin I should try? I'm still pretty stuck myself at the moment but glad I'm not the only one.

    @kim,

    I suppose developing my sites against the whole umbraco solution with full source code would be one option. But the impression I keep getting is that I should be able to build a site from the Codeplex binaries pacakage. Especially if this is going to impact how upgrades are performed in the future and not require me to keep updating source code trees and re-compiling a custom build everytime.

    Thank you both for your replies. Would love to get some more feedback from others on this!!

  • Funka! 398 posts 661 karma points
    Jun 12, 2013 @ 19:31
    Funka!
    2

    @Peter,

    After much trial and error I found out how to actually get custom routing to work. I found the documentation page to be a good start but seems to be missing in my opinion the critical parts I needed to actually build anything that works. For example, "Where to put your routing logic" doesn't actually touch on the fact regarding how global.asax.cs isn't something you can edit unless you want to compile your own custom build. Well I found a way around this, hopefully this helps:

    Create a new file /App_Code/global.asax.cs. Here's what mine looks like (change "LocationsAPI" to whatever you want, of course.):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    public class CustomUmbracoApplication : Umbraco.Web.UmbracoApplication
    {
        protected override void OnApplicationStarted(object sender, EventArgs e) 
        {
            // Add your own custom routing in here... Only route what you need, don't cut off Umbraco
            RouteTable.Routes.MapRoute(
                "LocationsApi",
                "locationsApi/{latitude}/{longitude}",
                new { controller = "Locations", action = "Index" }
            );
            base.OnApplicationStarted(sender, e);
        }
    }

    Now the fun (and most important!) part. Edit the existing file /global.asax:

    Change the one line in this file from this:

    <%@ Application Codebehind="Global.asax.cs" Inherits="Umbraco.Web.UmbracoApplication" Language="C#" %>

    To instead say:

    <%@ Application Codebehind="Global.asax.cs" Inherits="CustomUmbracoApplication" Language="C#" %>

    Bam! You've now side-stepped the original global.asax.cs and can now use your own without having to recompile umbraco. This took me waaaaay too long to figure out, I'm embarrassed to say. Still more to figure out though!

  • Richard Bailey 1 post 22 karma points
    Jun 19, 2013 @ 13:39
    Richard Bailey
    1

    Hi All

    This is where I am up to.

    I needed my own MVC app with Umbraco to just handle content pages (so I could have my own routing alongside Umbraco's automatic routing).  I started with an MVC 4 project in VS.  I then used nuget to grab Umbraco (6.1.1 in my case).  Thinking that customer controllers were the way to create my own controllers (which would therefore somehow match the routes I wanted for my app) I started with creating those, but couldn't get it to work as I had expected.  The point I had missed in all the documentation was that in order for a custom controller to work you appear to have to have a doctype in Umbraco to match it.  That wasn't what I wanted - I wanted my controllers to be able to access my non-umbraco data source.

    Anyway, aside from that I was also looking at how to handle my own routes - Umbraco likes to take over, and as I understand it ignores global.asax.  The answer came from http://aaron-powell.com/posts/2012-06-12-using-mvc-in-umbraco-4.html and his follow up post http://aaron-powell.com/posts/2012-07-11-using-mvc-in-umbraco-4-revisited.html  Aaron describes how he creates a completely empty project from scratch but because I already had a MVC4 project I was able to use his technique of updating the routing in App_Start folder, in particular this line [assembly: PreApplicationStartMethod(typeof(WebApplication2.App_Start.RouteSetup), "Setup")]

    I think this is a MVC4 project approach by VS that you may not have if using the MVC3 etc templates.

     So I now have a mix of Umbraco content and my own controllers.  I can use the Umbraco API to access content from my own controllers (with help from  http://www.ben-morris.com/using-umbraco-6-to-create-an-asp-net-mvc-4-web-applicatio ) and I have been able to access my non-umbraco data from Umbraco pages using the customer controllers.

    I am at the stage where I'm about to start working on the views and sharing layouts between my controller pages and the umbraco ones, so hopefully not too painful!

    The problem I do have is that while everything works fine in my uncompiled code when I deploy my site (I use Team City to run my sln and deploy project) I got a few errors.  First was that I was getting a "folder app_code can't exist in a compiled site" error which was resolved by getting rid of the global.asax files (I had read that Umbraco was ignoring them anyway).  Once that had gone away the problem I am now getting (and can't figure out at the moment) is that I get this error.

    Object reference not set to an instance of an object. 

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

     Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    Source Error:  An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

    Stack Trace: 
    [NullReferenceException: Object reference not set to an instance of an object.]
       Umbraco.Web.UmbracoModule.BeginRequest(HttpContextBase httpContext) +25
       System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +79
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +269

     Hopefully I've helped you, and if anyone can fire back any ideas about why I'm getting this error it would be very much appreciated

    Thanks

    Richard

     EDIT - It seems that deleting my global.asax is the cause of my problem so don't do that!!!!  

     

  • Patrick Scott 70 posts 110 karma points c-trib
    Jun 19, 2013 @ 16:25
    Patrick Scott
    1

    I've been creating sites with v6 and MVC using Visual Studio 2012, the way I have set this up is:

    • put the UMBRACO site and a custom project (I think I used a Web Application) in the same solution
    • set your custom project to be the startup project, all the below is for the custom project
    • on the build/configuration menu set just the custom project to build
    • in the project properties, web tab - set "use custom web server" option and enter the URL of your site
    • either set a page to start on or I set it to don't open a page.
    • I think this is in a different place on VS 2010, but on the compile tab, click on the build events button, here you can set post build command lines, mine is set to

     

    XCOPY "$(ProjectDir)bin\SPC_Umbraco_Dating_Extensions.*" "$(ProjectDir)..\umbraco\bin\" /y
    XCOPY "$(ProjectDir)usercontrols\*.ascx" "$(ProjectDir)..\umbraco\usercontrols\" /y /e
    XCOPY "$(ProjectDir)masterpages\*.master" "$(ProjectDir)..\umbraco\masterpages\" /y /e
    XCOPY "$(ProjectDir)views\*.cshtml" "$(ProjectDir)..\umbraco\views\" /y /e
    XCOPY "$(ProjectDir)views\*.vbhtml" "$(ProjectDir)..\umbraco\views\" /y /e

    this copies my custom code over to the umbraco website whenever I build the project.

    If you are going to write MVC views in your custom project then you will have to setup a web.config in the views directory and maybe some stuff in the main web.config also.

     

    Hope this helps.

     

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Jun 19, 2013 @ 17:33
    David Brendel
    2

    The way I build v6 sites in Visual Studio is as following:

    • Create a new MVC4 project in visual studio
    • Delete the web.config files and the Global.asax
    • Use Nuget to install the latest Umbraco version
    After this i just can create my controllers and models in the right folders and startup the hole site in debug mode.
    But this is used for building an app that not use custom routes. Until now I never used custom routes.
    Just one time I used areas to integrate a finished mvc app to umbraco so that the app could use the same database. But there I don't use content from Umbraco besides the members.
  • Patrick Scott 70 posts 110 karma points c-trib
    Jun 20, 2013 @ 10:07
    Patrick Scott
    0

    My umbraco sites are deployed to a VPS server and the initial load time of the IIS site is always an issue (the cpu assigned to this VPS is not that big). So my focus is getting all my custom code compiled before deployment, thats why I avoid putting any code into the umbraco project directly.

    I have not made custom routes, but I cannot see why it would not work whether the class was in the umbraco project or an external project.

    Has anyone got any ideas on how to compile more of umbraco or the MVC views before deployment? Any tips on speeding up the initial site load time?

    I do use the binary builds of umbraco, but looking at task manger when the site is first loaded there is lots of calls to csc.exe

  • Peter Laurie 8 posts 27 karma points
    Jun 23, 2013 @ 22:42
    Peter Laurie
    0

    @Funka! and Richard Bailey

    Thank you for your responses, they have been very helpful in giving me insite into the application start. I am about to use your feedback Richard in my lastest project at work.

    Than you again.

    Pete

  • Funka! 398 posts 661 karma points
    Jul 16, 2013 @ 02:04
    Funka!
    0

    Thank you to everyone that replied! For posterity, I wanted to follow up on my own topic but am hesitating to mark anything in this thread as an "answer" because there does seem to be many different ways to do this. For me, the approach I eventually found to be the easiest and quickest to get up and running was that suggested by David B. -- create a new MVC4 project in Visual Studio and then use NuGet to load umbraco into the project.  This allows me to develop my files in whatever folders I want and everything gets compiled together nicely. (I haven't actually deployed from this yet but at least I have things running locally.)

    Otherwise, if developing against the official binaries, it seems you will need to develop everything within the App_Code folder, but then (I think??) you lose out on Visual Studio debugging. A variation on this (I think, as I never actually got this to work) is to keep official binaries in its own no-compile "Web Site" and add your own MVC project to the solution. But then you need to add a bunch of umbraco references back into your MVC project and vice versa, so it just seemed easier to me to combine into one big project using the NuGet approach. I suppose if I was doing something small, quick and dirty, I might resort to just doing stuff in App_Code, but for anything larger I don't think that would sound appealing.

    Thanks again everyone!

     

  • Kevin Lawrence 183 posts 350 karma points
    Jul 16, 2013 @ 10:52
    Kevin Lawrence
    1

    I second David B's approach, which i have used for many projects now.  You can put your code into Controllers/Model/Views folders and everything works fine, I also haven't used custom routes and have yet to encounter a reason to do so.

    You lost VS compilation when putting everything in App_Code, that folder is not designed to be used in the IDE, it's usually for code that will be compiled at runtime.

  • Jerode 44 posts 159 karma points
    Aug 18, 2013 @ 16:35
    Jerode
    0

    The following setup for me is pretty easy with multiple developers working on it.

    Solution file:

    • Project 1: Umbraco Web
    • Project 2: Custom Controls (class library)
    • -->Models
    • -->SurfaceControllers
    • -->ViewModels
    Go to your references folder in the Umbraco web and right click and add reference. Select the section Solution and then select your Custom Controls project. You can debug, put breakpoints on your partial views in the Umbraco web and following execution path throughout.
    The references I needed in the Custom Controls were:
    • businesslogic
    • cms
    • interfaces
    • log4net
    • umbraco
    • umbracoCore
    SurfaceController class header example:
    public sealed class ContactUsSurfaceController : SurfaceController
    {
    [HttpPost] [ValidateAntiForgeryToken] public ActionResult SaveContactUsInformation(ContactUsViewModel model) {
    }

    Call it in your partial view like so:

    @using (Html.BeginForm("SaveContactUsInformation", "ContactUsSurface"))
    {

    I can provide more details if you need.

    Sincerely,
    Jerode

  • Robin Hjelmeir 24 posts 85 karma points
    Jan 14, 2014 @ 17:58
    Robin Hjelmeir
    0

    Here is a how-to for setting up umbraco 6 in VS 2012 with mvc: http://www.systenics.com/blog/setting-up-umbraco-6-in-visual-studio-2012-for-mvc-development/

  • ajexpress 4 posts 24 karma points
    Jan 31, 2014 @ 17:50
    ajexpress
    0

    Jerode,

    I have my project set up as

    - Umbraco website hosted on IIS (pointed to umbraco database)

    - Existing Custom MVC 4 application (set up as a virtual directory inside umbraco website; pointed to the separate database)

    I followed this article to integrate Umbraco with MVC application. I am having issues accessing my MVC website as it always says Cannot open the database.

    Please help if you have any idea how to run separate database for Umbraco and MVC application.

    Thanks.

     

     

  • Jerode 44 posts 159 karma points
    Jan 31, 2014 @ 18:09
    Jerode
    0

    Do you have the umbracoDbDSN connection string defined in the web.config? If your MVC app is accessing Umbraco binaries, add the umbracoDbDSN to its connection strings as well.

    You could also check the App_Data/logs folder and see if the Umbraco logs share any insight. 

  • ajexpress 4 posts 24 karma points
    Jan 31, 2014 @ 20:20
    ajexpress
    0

    Thanks Jerode. Finally I got it working.

    Do you know if we can import the view from my asp.net mvc application to umbraco site and still access the controllers and models from my custom mvc application?

    Can you provide some more details about how you use the Custm library models, viewmodels from the umbraco web?

    Thanks.

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 31, 2014 @ 20:30
    Jeroen Breuer
    0

    If you need more examples of MVC and Umbraco check the Hybrid Framework: http://our.umbraco.org/projects/developer-tools/hybrid-framework

    Jeroen

  • ajexpress 4 posts 24 karma points
    Jan 31, 2014 @ 22:38
    ajexpress
    0

    Will do. Thanks Jeroen.

  • Ben Weeks 14 posts 35 karma points
    Sep 01, 2015 @ 14:19
    Ben Weeks
    0

    Hi Jerode & Funka,

    Is this still the preferred way with v7? I have a similar issue I wondered if you might be able to help with:

    https://our.umbraco.org/forum/developers/api-questions//71065-dll-reference-for-umbracowebumbracoapplication

    Regards,

    Ben

  • Jerode 44 posts 159 karma points
    Sep 01, 2015 @ 14:47
    Jerode
    0

    It is still my preferred way and was no problem adding the updated dll's and files for v7.

Please Sign in or register to post replies

Write your reply to:

Draft