ASP.NET MVC using App_Code directory

吃可爱长大的小学妹 提交于 2019-12-04 15:50:52

You could try changing the files "Build Action" in Properties from "Compile" to "Content". Then they would be compiled like a WebSite. The article below explains it:

http://vishaljoshi.blogspot.co.uk/2009/07/appcode-folder-doesnt-work-with-web.html

ASP.NET MVC uses a web application project in contrast to a web site. A web application project needs to be compiled. The App_Code directory makes no sense in such application type.

It seems unlikely that I can reference code in my MVC web app assembly from App_Code at design time. I believe this is just the nature of Web Application projects in Visual Studio and the fact that code in the App_Code directory is being compiled into a different assembly.

In my original question I explained that I was wanted to use App_Code because of it's dynamic compilation capabilities. Thinking about my extensibility requirements, the fact that intellisense doesn't work property should not be a concern as the whole point is that an IDE is not required to develop plugins - if I was going to open up Visual Studio to develop them I may as well just use class libraries.

So thinking about my plugin architecture, I am fine with the notion of defining my plugins (http://weblogs.asp.net/justin_rogers/articles/61042.aspx) and I can do automatic loading of plugins in a specific directory like so:

            var assemblies = new List<Assembly>();
        var di = new System.IO.DirectoryInfo(Server.MapPath("~/Plugins"));

        di.GetFiles("*.dll").ToList().ForEach(x => {
            assemblies.Add(Assembly.LoadFrom(x.FullName));
        });

        List<Plugin> ExternalPlugins =
            Plugin.InitializePlugins(assemblies).ToList();

The only reason for not using /bin was performance. However, since the plugin project referenced the main web project I ended up having to use post build events to keep everything in check - which I didn't like.

So a better solution (as suggested by a number of people) is to use a configuration file to define plugins and drop the dlls into bin as normal.

But with all these different approaches I am skirting round the initial requirement - to be able to tweak plugins on the fly using no IDE and without a need to manually compile the application.

So in this case, is using App_Code really so bad and will it come back to bite me?...

There is an answer. For MVC3 at least. Do not open the solution as a solution. Open it as a website from local IIS (assuming that's how you run it). Then you will see your dynamic app_code code showing up with intellisense. But you won't be able to browse to any other code libraries that are outside. You'll need another instance of studio and opening as a solution to do that.

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