Copied to clipboard

Flag this post as spam?

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


  • Henrik 2 posts 92 karma points
    Feb 07, 2019 @ 13:48
    Henrik
    0

    Could not find a Surface controller route in the RouteTable for controller name Login

    I have this small login function

        [HttpPost]
        public ActionResult ValidateLogin(RCS_CustomerContact model)
        {
            Session["userID"] = null;
            using (RCS.Models.SAPContext context = dbContext)
            {
                if (model.E_MailL != null && model.Password != null)
                {
                    RCS_CustomerContact user = context.CustomerContact.Where(a => a.E_MailL == model.E_MailL && a.Password == model.Password).FirstOrDefault();
                    if (user != null)
                    {
                        Session["userID"] = user.ID;
                        Response.Redirect("/login/profile/");
                    }
                    else
                    {
                        TempData["LoginMessage"] = "Invalid user/password";
                        Session["userID"] = null;
                    }
                }
                else
                {
                    TempData["LoginMessage"] = "Fill username and password";
                    Session["userID"] = null;
                }
            }
            return RedirectToCurrentUmbracoPage();
        }
    

    And this partial

    @model RCS.Models.RCS_CustomerContact
    
    @using (Html.BeginUmbracoForm("ValidateLogin", "Login", FormMethod.Post))
    {
    <section>
        <div>
            <table>
                <tr>
                    <td>@Html.LabelFor(a => a.E_MailL):</td>
                    <td>
                        @Html.TextBoxFor(a => a.E_MailL)
                    </td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(a => a.Password):</td>
                    <td>@Html.TextBoxFor(a => a.Password)</td>
                </tr>
            </table>
            <input type="submit" value="Login">**
    
            @TempData["LoginMessage"]
    
    
        </div>
    </section>
    }
    

    The user goes to www.mysite.com/login/ Here he sees the login partial, fills it out and login.

    This works perfectly fine. No problem what so ever... Subpages like "profile" and "orderhistory" also works fine.

    Now I need to add a custom route to /login/order/[orderNum] so the user can click an order in "history" and see the details.

    So I added this Route (I also have a route to product details)

    public class CustomRoutes: ApplicationEventHandler
    {
        protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            RouteTable.Routes.MapUmbracoRoute(
                "ProductDetails",
                "Product/{itemInfo}",
                new
                {
                    controller = "Product",
                    action = "ProductDetails",
                    itemID = UrlParameter.Optional
                },
                new FindProductRouteHandler()
            );
            RouteTable.Routes.MapUmbracoRoute(
                "OrderDetails",
                "login/order/{orderNum}",
                new
                {
                    controller = "Login",
                    action = "OrderDetails",
                    itemID = UrlParameter.Optional
                },
                new FindOrderRouteHandler()
            );
            base.ApplicationStarting(umbracoApplication, applicationContext);
    
        }
    }
    
    
    public class FindOrderRouteHandler : UmbracoVirtualNodeRouteHandler
    {
        protected override IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext)
        {
    
            var productPage = umbracoContext.ContentCache
                .GetAtRoot()
                .First()
                .FirstChild(a => a.Name.ToLower() == "login")
                .FirstChild(a => a.Name.ToLower() == "orderdetail");
    
            return productPage;
        }
    }
    

    So now I have a route, and can browse to /login/order/123 and see the order. Perfect...

    But now my login doesn't work. What?? I can go to /login/ and see the login form, but when I try to login I get this error:

    Could not find a Surface controller route in the RouteTable for controller name Login
    

    If I remove the route my login works again:

        //RouteTable.Routes.MapUmbracoRoute(
        //    "OrderDetails",
        //    "login/order/{orderNum}",
        //    new
        //    {
        //        controller = "Login",
        //        action = "OrderDetails",
        //        itemID = UrlParameter.Optional
        //    },
        //    new FindOrderRouteHandler()
        //);
    

    Can someone tell me why? And what to do to fix it?

    I am kind off new to Umbraco. Only worked with it for a few months.

  • Henrik 2 posts 92 karma points
    Feb 07, 2019 @ 15:36
    Henrik
    100

    Ok.. I found a workarround.

    I made a new "LoginRoute" controller, and pointed my route to that:

        RouteTable.Routes.MapUmbracoRoute(
            "OrderDetails",
            "login/order/{orderNum}",
            new
            {
                controller = "LoginRoute",
                action = "OrderDetails",
                itemID = UrlParameter.Optional
            },
            new FindOrderRouteHandler()
        );
    

    Apparently you can't have this:

    public ActionResult OrderDetails(RenderModel model, string orderNum)
    {
       //Code
    }
    
    public ActionResult ValidateLogin(RCS_CustomerContact model)
    {
      //Code
    }
    

    In the same surfacecontroller when one ActionResult is used in a custom route.

    Are the any recommended way of creating Controllers for custom routes? I will have to make a few more, so I'm wondering if I should simply make a "CustomRoute" controller, and let that handle all custom routes. But that means that my code will be spilt up, so all Actions related to Login will not be placed in the LoginController, since a few will be in the CustomRouteController.

  • Harry Spyrou 199 posts 560 karma points
    Feb 07, 2019 @ 15:46
    Harry Spyrou
    0

    Have you checked Umbraco Route hijacking?

Please Sign in or register to post replies

Write your reply to:

Draft