Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App_Code folder created automatically in published website

I have a MVC4 App created in VS 2010 with Umbraco 6 too and I've created a web deploy project which is used by my Team City CI server to deploy my website to a CI environment for testing.

On the CI server the first time I load the homepage (or any page) it loads perfectly fine. However the act of loading a page creates a App_Code folder on my CI server, and after that I get the message "The directory '/App_Code/' is not allowed because the application is precompiled". Removing the App_Code folder means that it once again works for one page load and the folder is created again.

Removing the PrecompiledApp.config file causes my site to not load with a YSOD stating "Object reference not set to an instance of an object." at the following point in the stack trace "Umbraco.Web.UmbracoModule.BeginRequest(HttpContextBase httpContext) +25"

To be clear, I don't have an App_Code folder in my project, and I don't want or need one. All I want is for it to stop creating one automatically upon page load! I've used Umbraco in VS and deployed in the same way many times before, just not with Umbraco 6 and in an MVC project.

Any ideas why App_Code is being automatically created and what I can do to stop it?

Many Thanks

Richard

like image 878
richybailey Avatar asked Jun 28 '13 16:06

richybailey


People also ask

What is the App_Code folder?

The App_Code folder can contain source code files written as traditional class files — that is, files with a . vb extension, . cs extension, and so on. However, it can also include files that are not explicitly in a specific programming language.

What is use of App_Data and App_Code folder in asp net?

Contains application data files including . mdf database files, XML files, and other data store files. The App_Data folder is used by ASP.NET to store an application's local database, such as the database for maintaining membership and role information.

Can the App_Code folder contain source code files in different programming languages?

Can the App_Code folder contain source code files in different programming languages? Answer: Yes but you need to create two subfolders inside the App_Code and then add both C# and VB.NET in the respective subfolders. You also have to add configuration settings in the web.


1 Answers

I seem to use Umbraco in a similar way as you do, wrapping it as a MVC 4 project. Hence it becomes a "VS Web Application" instead of a "VS Web Site".

What is important to remember is that Umbraco originally wasn't made to be run as an application and a lot of the functionality in Umbraco is first and foremost directed to using App_Code.

The internal classes AssemblyExtensions, PluginManager, TypeHelper and the public class TypeFinder in Umbraco.Core have methods that are dependent on that the App_Code folder is there. Even if you don't need an App_Code in your solution Umbraco does, if you don't want to see it simply hide it from your solution. If you really don't want it remove all references to it in the source and create your own compilation of Umbraco.

== EDIT BELOW ==

After reading your comment and post again I created a small solution for your problem. The fact that Umbraco creates the App_Code is still because of the framework initialization that won't work without App_Code existing. But recompiling and creating an own dist of Umbraco will as OP points out create some extra maintenance when upgrading and so on.

It's not the ideal but most clean way to handle this matter to allow Umbraco to create the App_Code folder but also remove the same when the application have initialized. I'd use an IApplicationEventHandler. The sample code works on my box.

using Umbraco.Core;
using Umbraco.Core.IO;

namespace YourNamespace.EventHandlers
{
    public class AppCodeEvents : IApplicationEventHandler
    {
        public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        { }

        public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        { }

        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            if (System.IO.Directory.Exists(IOHelper.MapPath(IOHelper.ResolveUrl("~/App_Code"))))
            {
                System.IO.Directory.Delete(IOHelper.MapPath(IOHelper.ResolveUrl("~/App_Code")));
            }
        }
    }
}
like image 154
Eric Herlitz Avatar answered Sep 27 '22 22:09

Eric Herlitz