Copied to clipboard

Flag this post as spam?

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


  • May Zhang 11 posts 61 karma points
    Feb 21, 2023 @ 22:16
    May Zhang
    0

    Umbraco 10: no service for type "MySurfaceController" has been registered

    Calling surface controller action in component view as below:

    @using (Html.BeginUmbracoForm<myprojects.Controllers.MySurfaceController>("LoginAsync")))
    

    Got the following error after submitting the form:

    InvalidOperationException: No service for type 'myprojects.Controllers.MySurfaceController' has been registered
    

    Stack info: enter image description here

    Any idea how to fix it? Thanks!

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Feb 22, 2023 @ 10:05
    Dave Woestenborghs
    0

    Hi May,

    Can you show us the code of the controller ?

    Dave

  • May Zhang 11 posts 61 karma points
    Feb 22, 2023 @ 18:02
    May Zhang
    0

    Hi Dave, here it is

    public class MySurfaceController: SurfaceController
    {
        private ILogger<MySurfaceController> _logger;
        public MySurfaceController(ILogger<MySurfaceController> logger,
                                   IUmbracoContextAccessor umbracoContextAccessor,
                                   IUmbracoDatabaseFactory databaseFactory,
                                   ServiceContext services,
                                   AppCaches appCaches,
                                   IProfilingLogger profilingLogger,
                                   IPublishedUrlProvider publishedUrlProvider)
            : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
        {
            _logger = logger;
        }
    
        [HttpPost]
        [ValidateAntiForgeryToken]
        public  IActionResult Login(LoginViewModel login)
        {
            // some logic here
            return RedirectToUmbracoPage(CurrentPage.Root());
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> LoginAsync(LoginViewModel login)
        {
            // some logic here
            return RedirectToUmbracoPage(CurrentPage.Root());
        }
    }
    
  • Huw Reddick 1736 posts 6076 karma points MVP c-trib
    Feb 22, 2023 @ 11:18
    Huw Reddick
    0

    I just had this same issue myself, I narrowed it down to some kind of weird routing issue and if I set the forms action to a valid view url it then posted to the surface controller quite happily

    So for example I did this

    @using (Html.BeginUmbracoForm<MembershipSurfaceController>("SubmitSigninForm", null, new { id = "submitsigninform", action = "/login"}, FormMethod.Post))
    {
        .....
    }
    

    Where "/login" is the actual view that hosts the viewcomponent

  • May Zhang 11 posts 61 karma points
    Feb 24, 2023 @ 17:32
    May Zhang
    100

    The original Umbraco 10 project was created from VisualStudio:

    Create a new project -> Umbraco Project(Umbraco HQ)

    I am able to resolve this issue by re-creating the solution/project from command line ( https://psw.codeshare.co.uk/):

    dotnet new sln --name "MySolution" dotnet new umbraco --force -n "MyProject" --friendly-name "Administrator" --email "[email protected]" --password "1234567890" --development-database-type SQLite dotnet sln add "MyProject"

    Thanks for all your helps!

  • Olajire Oluwafemi 1 post 71 karma points
    May 15, 2023 @ 22:04
    Olajire Oluwafemi
    0

    Hello everyone, I am having this same problem. What can I do please? This is the LoginController code:

    using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Umbraco.Cms.Core.Cache; using Umbraco.Cms.Core.DependencyInjection; using Umbraco.Cms.Core.Logging; using Umbraco.Cms.Core.Routing; using Umbraco.Cms.Core.Security; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Web; using Umbraco.Cms.Infrastructure.Persistence; using Umbraco.Cms.Web.Common.ActionsResults; using Umbraco.Cms.Web.Common.Filters; using Umbraco.Cms.Web.Common.Models; using Umbraco.Cms.Web.Common.Security; using Umbraco.Cms.Web.Website.Controllers; using Umbraco.Extensions; using SignInResult = Microsoft.AspNetCore.Identity.SignInResult;

    namespace AmanaCard.Controllers { public class LoginController : SurfaceController {

        private readonly IMemberManager _memberManager;
        private readonly IMemberSignInManager _signInManager;
        private readonly ITwoFactorLoginService _twoFactorLoginService;
    
        [ActivatorUtilitiesConstructor]
        public LoginController(
            IUmbracoContextAccessor umbracoContextAccessor,
            IUmbracoDatabaseFactory databaseFactory,
            ServiceContext services,
            AppCaches appCaches,
            IProfilingLogger profilingLogger,
            IPublishedUrlProvider publishedUrlProvider,
            IMemberSignInManager signInManager,
            IMemberManager memberManager,
            ITwoFactorLoginService twoFactorLoginService)
            : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
        {
            _signInManager = signInManager;
            _memberManager = memberManager;
            _twoFactorLoginService = twoFactorLoginService;
        }
    
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateUmbracoFormRouteString]
        public async Task<IActionResult> HandleLogin([Bind(Prefix = "loginModel")] LoginModel model)
        {
            if (ModelState.IsValid == false)
            {
                return CurrentUmbracoPage();
            }
    
            MergeRouteValuesToModel(model);
    
            // Sign the user in with username/password, this also gives a chance for developers to
            // custom verify the credentials and auto-link user accounts with a custom IBackOfficePasswordChecker
            SignInResult result = await _signInManager.PasswordSignInAsync(
                model.Username, model.Password, model.RememberMe, true);
    
            if (result.Succeeded)
            {
                TempData["LoginSuccess"] = true;
    
                // If there is a specified path to redirect to then use it.
                if (model.RedirectUrl.IsNullOrWhiteSpace() == false)
                {
                    // Validate the redirect URL.
                    // If it's not a local URL we'll redirect to the root of the current site.
                    return Redirect(Url.IsLocalUrl(model.RedirectUrl)
                        ? model.RedirectUrl
                        : CurrentPage!.AncestorOrSelf(1)!.Url(PublishedUrlProvider));
                }
    
                // Redirect to current URL by default.
                // This is different from the current 'page' because when using Public Access the current page
                // will be the login page, but the URL will be on the requested page so that's where we need
                // to redirect too.
                return RedirectToCurrentUmbracoUrl();
            }
    
            return CurrentUmbracoPage();
    
        }
    
        private void MergeRouteValuesToModel(LoginModel model)
        {
            if (RouteData.Values.TryGetValue(nameof(LoginModel.RedirectUrl), out var redirectUrl) && redirectUrl != null)
            {
                model.RedirectUrl = redirectUrl.ToString();
            }
        }
    }
    

    }

  • May Zhang 11 posts 61 karma points
    May 16, 2023 @ 12:31
    May Zhang
    0

    I always got the same error if creating the project from VS umbraco 10 project.

    Please try re-creating your project using the command below, and then add your controllers, etc. back.

    dotnet new sln --name "MySolution" dotnet new umbraco --force -n "MyProject" --friendly-name "Administrator" --email "[email protected]" --password "1234567890" --development-database-type SQLite

    dotnet sln add "MyProject"

Please Sign in or register to post replies

Write your reply to:

Draft