Copied to clipboard

Flag this post as spam?

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


  • Kristian Overgaard 22 posts 184 karma points notactivated
    Sep 20, 2019 @ 08:01
    Kristian Overgaard
    0

    Create custom controller

    Hi.

    I'm getting started with Umbraco, and i'm looking to create a custom controller. I've found multiple questions about this on the forums, but I think I'm missing something essential, for the answers to work for me. I've found the following documentation for adding a controller: https://our.umbraco.com/documentation/reference/routing/WebApi/#locally-declared-controller https://our.umbraco.com/documentation/reference/routing/custom-controllers

    But these seem to assume that Umbraco is running in an MVC like c# project.

    The way i got my Umbraco solution up and running is by downloading it from here: https://our.umbraco.com/download/releases/ And then opening the root folder with visual code. Which allowed me to run the site, and complete the initial Umbraco setup, and create content, through the backoffice.

    There's no MVC like structure, og c# files in the project, and when i tro to create a c# file, Visual code complains that no .csproj or project.json file exists.

    Is it not possible to add custom controllers, written in C#, when using Umbraco this way, and is it required to install Umbraco via Nuget, to have this option available ?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Sep 21, 2019 @ 08:21
    Alex Skrypnyk
    0

    Hi Kristian

    Of course, it's possible to add a custom controller into setup you have.

    Just add App_Code folder into the root of the project and create controller classes there.

    You opened Umbraco as a website.

    Thanks,

    Alex

  • Kristian Overgaard 22 posts 184 karma points notactivated
    Sep 22, 2019 @ 08:51
    Kristian Overgaard
    0

    This almost works, but the custom controllers can't recognize Model classes from the Umbraco.Web.PublishedModels namespace.

    In App_Data there's all.generated.cs and models.generated.cs contained types i have defined in Umbraco, however when i try to make use of these types in a custom controller i get the error: "CS0234: The type or namespace name 'MyClass' does not exist in the namespace 'Umbraco.Web.PublishedModels' (are you missing an assembly reference?)"

    in my custom class i have a helpermethod like so:

    using Myproj.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Web;
    using Umbraco.Web.Mvc;
    using Umbraco.Web.PublishedModels;
    
    namespace Myproj.Helper
    {
        public class LoanFilterHelper
        {
            public static List<Umbraco.Web.PublishedModels.MyClass> GetFilteredList(IEnumerable<IPublishedContent> MyClasses, FilterResponseModel filter, string culture = null)
            {
                List<MyClass> result = new List<Loan>();
    ...
    

    and the class is defined in both model and all.generated classes like so:

    [PublishedModel("loan")]
        public partial class Loan : LoanContainer
        {
    ...
    

    within the namespace:

    namespace Umbraco.Web.PublishedModels
    {
    

    am i missing something to let my custom classes and controllers know about the namespace with umbraco generated types ?

Please Sign in or register to post replies

Write your reply to:

Draft