How to add custom code - can I use app_code folder?
Does Umbraco v9 allow us to add custom cs files in the app_code folder? I have sites that are deployed to an IIS server and I sometimes need to add small pieces of functionality, and rather than do a complete new build & publish, I'd just like to add a CS file, similar to previous versions of Umbraco. Is this still possible in Umbraco v9?
No, you can't, App_Code is no longer a thing in ASP.NET Core. All the code that's part of your project needs to be built before it's published. Luckily, publishing code is also a lot lighter-weight in ASP.NET Core. :-)
Thanks Sebastiaan, makes sense. So if I wanted to add some custom code to hide the 'redirect URLs' dashboard, where would I place a custom class file in the project so that it builds into the project?
When I do Umbraco 7/8 Dev, I have the ".Web" project (with the website root files) , and then I have a separate Class library project ".Core" with all my .cs files, generally compiling into a single .dll in the ".Web/bin/" folder.
I am a bit unclear as to how best to replicate a similar setup in an Umbraco 9 project. Am I supposed to just treat the main project as the ".Core" project - creating subfolders for Models, etc. as I used to do for my separate class library? Or does it make sense to have a separate csproj for code linked as well?
I've had feedback from people that they like to hold on to what they used to do in v8, but the general way of working for many (non-umbraco) dotnet core programmers is to use only a single project. However, if you're developing a class library that is reusable for other projects as well then you could add an additional project.
When just building a website, the custom code you write is usually specific to that website and doesn't need to be in a generic class library as on your next project you probably won't be able to use (all of) that same code anyway.
And when I say such things, the feedback usually is something like; "but I like the separation and that's how I've always done it".
I would recommend just trying it out for a project or two, resist adding more than one project in your solution and see if you also learn to like that way of working.
But if you just want to do what you did in v8 then that's fine as well of course. Add a .Core project (dotnet core class library), reference it in you .Web project, and only install the NuGet packages that you NEED in the .Core project (so do not install Umbraco.Cms because that just installs the complete CMS in your class library). Generally I think you'll find you only need Umbraco.Cms.Web and possibly Umbraco.Cms.Core.
But.. as I said, try to resist for a project and see how you like it. 😊 (I should make it more like clickbait.. "and you won't believe what happened next.. 😮").
It's good to know what the generally accepted standard is.
Since there are some separate folders defined for the umbraco bits, I could certainly try the "single project" approach, and create subfolders to manage the ".Core" code (Models, Helpers, etc.) I would assume that the Namespace can be set at the folder-level similar to how it works in a .Net Framework class project...
How to add custom code - can I use app_code folder?
Does Umbraco v9 allow us to add custom cs files in the app_code folder? I have sites that are deployed to an IIS server and I sometimes need to add small pieces of functionality, and rather than do a complete new build & publish, I'd just like to add a CS file, similar to previous versions of Umbraco. Is this still possible in Umbraco v9?
No, you can't,
App_Code
is no longer a thing in ASP.NET Core. All the code that's part of your project needs to be built before it's published. Luckily, publishing code is also a lot lighter-weight in ASP.NET Core. :-)Thanks Sebastiaan, makes sense. So if I wanted to add some custom code to hide the 'redirect URLs' dashboard, where would I place a custom class file in the project so that it builds into the project?
Anywhere you want! That's the beauty, it will build any .cs files it finds, in the whole project. 😁
Related somewhat to this...
When I do Umbraco 7/8 Dev, I have the ".Web" project (with the website root files) , and then I have a separate Class library project ".Core" with all my .cs files, generally compiling into a single .dll in the ".Web/bin/" folder.
I am a bit unclear as to how best to replicate a similar setup in an Umbraco 9 project. Am I supposed to just treat the main project as the ".Core" project - creating subfolders for Models, etc. as I used to do for my separate class library? Or does it make sense to have a separate csproj for code linked as well?
I've had feedback from people that they like to hold on to what they used to do in v8, but the general way of working for many (non-umbraco) dotnet core programmers is to use only a single project. However, if you're developing a class library that is reusable for other projects as well then you could add an additional project.
When just building a website, the custom code you write is usually specific to that website and doesn't need to be in a generic class library as on your next project you probably won't be able to use (all of) that same code anyway.
And when I say such things, the feedback usually is something like; "but I like the separation and that's how I've always done it".
I would recommend just trying it out for a project or two, resist adding more than one project in your solution and see if you also learn to like that way of working.
But if you just want to do what you did in v8 then that's fine as well of course. Add a
.Core
project (dotnet core class library), reference it in you.Web
project, and only install the NuGet packages that you NEED in the.Core
project (so do not installUmbraco.Cms
because that just installs the complete CMS in your class library). Generally I think you'll find you only needUmbraco.Cms.Web
and possiblyUmbraco.Cms.Core
.But.. as I said, try to resist for a project and see how you like it. 😊 (I should make it more like clickbait.. "and you won't believe what happened next.. 😮").
Thanks, Seb, for your perspective. 😁
It's good to know what the generally accepted standard is.
Since there are some separate folders defined for the umbraco bits, I could certainly try the "single project" approach, and create subfolders to manage the ".Core" code (Models, Helpers, etc.) I would assume that the Namespace can be set at the folder-level similar to how it works in a .Net Framework class project...
is working on a reply...