App_Code folder created automatically in published website

牧云@^-^@ 提交于 2019-12-05 22:47:15

问题


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


回答1:


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")));
            }
        }
    }
}



回答2:


If you are precompiling your site using a web deploy project, I assume you have all your references separated out from the project - which is a good thing. So, I think the simple answer here is don't precompile the site, just allow the web application to be built so that it pulls in the references and deploy the built project.

Personally, I find the best way to work with Umbraco v6 is via NuGet. Create an empty MVC4 project and use NuGet to add Umbraco v6. This will automatically sort out all the references for you. This is because those fantastic guys at Umbraco created two Nuget packages, one with the project files and the other with the core DLLs.

This means that when the site is built, the references are pulled in and updating the site is easy. It is just a matter of updating via NuGet.




回答3:


This was happening me as well, turned out it was because a 'precompiledApp.config' file had somehow made its way onto the production server... not sure how that happened but once I deleted and recycled web app this stopped happening.



来源:https://stackoverflow.com/questions/17369301/app-code-folder-created-automatically-in-published-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!