Copied to clipboard

Flag this post as spam?

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


  • Alexander Mounzer 8 posts 118 karma points
    Jul 01, 2021 @ 13:24
    Alexander Mounzer
    0

    RedirectToAction(); from a surfacecontroller

    Recently I made a post in Stackoverflow on a certain problem I am having.

    https://stackoverflow.com/questions/68207917/redirecttoaction-fails-with-umbraco-v8-surfacecontroller

    I have problems routing my RedirectToAction(); from a surfacecontroller -> action to a RenderMvcController -> action.

    I get the following error: No route in the route table matches the supplied values

    My plan is to send the loginViewModel to the other controller that will later call a service with help of some parameters I have stored on my loginViewModel.

    AuthenticateController.cs

       public class AuthenticationController : SurfaceController
       {
    
          [HttpPost]
          [ValidateAntiForgeryToken]
          public async Task<ActionResult> Login(LoginViewModel loginViewModel)
          {
            bool success = false;
            string ticket;
    
            try
            {
                var response = await _authenticationService.LoginUserAndGetTicketAsync(new AuthorizeCurrentUserInputModel()
                {
                    ndsUserName = loginViewModel.Username,
                    ndsUserPasswd = loginViewModel.Password
                });
                success = response.Item1;
                ticket = response.Item2;
    
            } catch (Exception e)
            {
                this.ModelState.AddModelError(String.Empty, @"Något gick fel.");
                return View("Login", loginViewModel);
            }
    
            if (success)
            {
                FormsAuthentication.SetAuthCookie(loginViewModel.Username, true);
                if (!Request.RawUrl.Contains("/mina-sidor/"))
                {
                    return Redirect("/");
                }
    
                return RedirectToAction("Index", "MinaSidor"); <--- returns No route registred
            }
            else
            {
                this.ModelState.AddModelError(String.Empty, @"Användarnamn eller lösenord är fel.");
            }
    
            return View("Login", loginViewModel);
        }
    }
    ...
    

    MinaSidorController.cs

        public class MinaSidorController : RenderMvcController
        {
          [HttpGet]
          public ActionResult Index(ContentModel model)
          {
            return View();
          }
         ...
    

    I have checked the forum and tried whats out there but none of it has helped me.

    What I have read: 1. no-route-in-the-route-table-matches-the-supplied-values 2. RedirectToAction correctly in surfacecontroller

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Jul 01, 2021 @ 23:11
    Marc Goodson
    100

    Hi Alexander

    When you inherit from RenderMvcController within Umbraco it's a bit different to a pure MVC experience.

    By default the controllers aren't autorouted via the Url you would expect in MVC and so returning Redirect To Action will report that a route cannot be founded.

    RenderMvcController are routed by convention...

    Eg any controller called DocTypeAliasController will handle any requests made to a page based on that particular Document Type (it's called route hijacking)

    Anyway if you have a document type called 'MinaSidor'... Then what you need to do is redirect to a published page that uses that document type.

    There is a special helper return method called RedirectToUmbracoPage that will allow you to achieve that from your surface controller...

    https://our.umbraco.com/Documentation/Reference/Routing/surface-controllers-actions#redirecttoumbracopage

    If you don't have a Document Type with that name... Then you could explicitly register a custom route for it... However you can see it needs the context of an Umbraco content item in the Index action result and to achieve this you use 'MapUmbracoRoute' instead of 'MapRoute' and the your Redirect To Action would 'just work' like standard mvc

    Regards

    Marc

  • Alexander Mounzer 8 posts 118 karma points
    Jul 02, 2021 @ 06:54
    Alexander Mounzer
    0

    Hi Marc,

    I tried register a route for MinaSidor and took the example in the umbraco documentation but it wont run.

    I get the following Exception:

    [Exception: No physical template file was found for template Index] Umbraco.Web.Mvc.RenderMvcController.CurrentTemplate(T model) in D:\a\1\s\src\Umbraco.Web\Mvc\RenderMvcController.cs:86

    public class RegisterCustomRouteComposer : ComponentComposer<RegisterCustomRouteComponent>
    { }
    
    public class RegisterCustomRouteComponent : IComponent
    {
        public void Initialize()
        {
            // Custom route to MyProductController which will use a node with a specific ID as the
            // IPublishedContent for the current rendering page
            RouteTable.Routes.MapUmbracoRoute(
                "MinaSidorCustomRoute",
                "mina-sidor/{action}/{id}",
                new
                {
                    controller = "MinaSidor",
                    id = UrlParameter.Optional
                }, new UmbracoVirtualNodeByIdRouteHandler(1145));
        }
    
        public void Terminate()
        {
            // Nothing to terminate
        }
    }
    

    As for using surfacecontroller and the autorouting rules i need to somehow pass my CustomModel that holds the user ticket.

    When I decide to change that inheritance : surfaceController I get:

    Cannot bind source type Umbraco.Web.Models.ContentModel to model type legimusWeb.ViewModels.LoginViewModel

    LoginViewModel // customModel

    SurfaceController wants me to send a ContentModel.

Please Sign in or register to post replies

Write your reply to:

Draft