Copied to clipboard

Flag this post as spam?

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


  • Mehul Gajjar 48 posts 172 karma points
    Apr 24, 2015 @ 09:31
    Mehul Gajjar
    0

    RenderModel get null using custom routing

    Hi,

    I have install umbraco 7.2.4 with starter kit and made below changes

    1) set custom route

    public class MyStartupHandler : IApplicationEventHandler
    {
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
    
     RouteTable.Routes.MapRoute(
                name: "news",
                url: "news/{*catchall}",
                defaults: new { controller = "umbNewsOverview", action = "Index", id = UrlParameter.Optional }
            );
    
       }
    

    2) below is my controller

      public class umbNewsOverviewController : RenderMvcController
        {
    
            public override ActionResult Index(RenderModel model)  //<- model = null when i used custom route.
            {
    
                 // some code
                 return view();
             }
    
         }
    

    Question : when i set custom route i get render model null :( , when i remove custom route i got value in Render model.

    can some body help me on this.?

    Thanks in advance

    Regards

    Mehul.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 24, 2015 @ 12:00
    Dave Woestenborghs
    0

    Hi,

    When you use route hijacking you don't need to set the route. Umbraco handles this for you.

    What you are trying to do is called route hijacking : https://our.umbraco.org/documentation/Reference/Templating/Mvc/custom-controllers

    Dave

     

  • James Jackson-South 489 posts 1747 karma points c-trib
    Apr 24, 2015 @ 17:14
    James Jackson-South
    0

    There are occasions where you might want to use custom routing without using route hijacking.

    • When you don't actually have nodes to match (virtual node) and you want nice urls. (virtual node), products, news articles etc.
    • You might want to reuse a single node for multiple pages.
    • You just want to actually have a route so you can use things like Url.Action("ActionResult", "Controller") which really useful when implementing membership and you want to redirect.

    In this case you can use UmbracoVirtualNodeRouteHandler

    You can use the overload of Umbraco.MapRoute which takes the handler as a parameter to provide a node to return which will populate the RenderModel.

    Here's my implementation of one for a virtual logout handler. The LoginRegisterPage class is simply a strong typed model that I convert to internally elsewhere.

    /// <summary>
    /// The logout virtual node route handler.
    /// </summary>
    public class LogoutVirtualNodeRouteHandler : UmbracoVirtualNodeRouteHandler
    {
        /// <summary>
        /// returns the <see cref="IPublishedContent"/> associated with the route.
        /// </summary>
        /// <param name="requestContext">
        /// The request context.
        /// </param>
        /// <param name="umbracoContext">
        /// The umbraco context.
        /// </param>
        /// <returns>
        /// The <see cref="IPublishedContent"/>.
        /// </returns>
        protected override IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext)
        {
            UmbracoHelper helper = new UmbracoHelper(umbracoContext);
            string alias = typeof(LoginRegisterPage).Name;
            return helper.TypedContentAtRoot()
                .First()
                .Descendants()
                .First(d => d.DocumentTypeAlias.InvariantEquals(alias));
        }
    }
    

    And the corresponding route.

    // Note the use of the "virtual" in the route handler name.
    // Logout doesn't exist in the back office.
    RouteTable.Routes.MapUmbracoRoute(
      "LogoutPage",
      "Logout/",
      new
      {
        controller = "LoginRegisterPage",
        action = "Logout"
      },
      new LogoutVirtualNodeRouteHandler()); 
    
  • Mehul Gajjar 48 posts 172 karma points
    Apr 27, 2015 @ 07:15
    Mehul Gajjar
    0

    Hi James,

    Thanks for reply.

    i will update my code as above.

    Regards

    Mehul Gajjar.

  • James Jackson-South 489 posts 1747 karma points c-trib
    Apr 27, 2015 @ 13:36
    James Jackson-South
    100

    No worries :)

    Let me know how you get on.

Please Sign in or register to post replies

Write your reply to:

Draft