+1-888-365-2779
Try Now
More in this section

MVC questions

3 posts, 0 answered
  1. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    15 Aug 2012
    Link to this post
    1) How do you generate a back link where the controller method isn't in the Url.  So if this is your page ~/items, then you click an item it goes to ~/items/Details/item-1.  So in that detail view I put this
    @Html.ActionLink("<< Back to Items", "Index")
    However then the Url changes to ~/items/Index.
    So that creates multiple Urls for the same content....?

    2) In the above example how do we control the route such that "Details" is "details" or ideally not even visible (is that possible? ~/items/item-1)

    Steve
  2. Denis
    Denis avatar
    105 posts
    Registered:
    27 Feb 2009
    12 Aug 2013 in reply to Steve
    Link to this post
    Hi Steve,

    My findings about this issue:
    1) Create custom MVC route to exclude action name from route:
    public static void RegisterRoutes(RouteCollection routes)
    {
        Bootstrapper.MVC.MapRoute("AgentsRoute", "Agents/{urlName}", new { controller = "Agents", action = "Detail", urlName = UrlParameter.Optional });
    }
    in above example I remove "Detail" part of URL
    2. Use custom route to generate appropriate URLs
    @Html.MyRouteLink("read more", "AgentsRoute", new 
                            {
                                controller = "Agents",
                                action = "Detail",
                                urlName = item.UrlName
                            })
    I created Extension funtionality like it is described here: http://net.tutsplus.com/tutorials/generating-traditional-urls-with-asp-net-mvc3/
    3) Now, in order to route Sitefinity to your controller and action, you will have to create and register Sitefinity custom route:
    public override System.Web.Routing.RouteData GetRouteData(HttpContextBase httpContext)
            {
                if (!VerifyRequest(httpContext))
                {
                    return null;
                }
                //get the path from the httpContext variable and parse it
                var virtualPathInternal = GetVirtualPathInternal(httpContext);
                if (virtualPathInternal != null)
                {
                    var siteMapProvider = GetSiteMapProvider();
                    if (siteMapProvider == null || ControlExtensions.IsBackend())
                    {
                        return null;
                    }
                    if (virtualPathInternal.EndsWith("Telerik.Web.UI.SpellCheckHandler.axd") ||
                        virtualPathInternal.EndsWith("Telerik.Web.UI.DialogHandler.aspx") ||
                        virtualPathInternal.EndsWith("ChartImage.axd"))
                    {
                        return null;
                    }
      
                    if (!virtualPathInternal.ToLower().StartsWith("agents/"))
                        return base.GetRouteData(httpContext);
      
                    bool isAdditional;
                    string[] pars;
                    var node = siteMapProvider.FindSiteMapNode(virtualPathInternal, false, out isAdditional, out pars);
      
                    if (node == null || pars == null || pars.Length == 0 || pars.Length > 2)
                        return base.GetRouteData(httpContext);
      
                    // Detail view
                    if (pars.Length == 1)
                    {
                        return GetRouteDataInternal(new[] { "Detail", pars[0] }, httpContext.Request.QueryString, node);
                    }
                }
                return null;
            }
    Above example transforms: agents/ivan-smith to agents/detail/ivan-smith

    Hope it helps,
    Denis.
  3. Tosho Toshev
    Tosho Toshev avatar
    8 posts
    Registered:
    15 May 2014
    15 Aug 2013
    Link to this post
    Hello Steve,

    There is not an easy way to hide the controller action name from your URL because our ActionInvoker class won't execute the action because of the path. It will search for the action in our internal dictionary and it will try to compare it with the "item-1" string instead of the real action name. Denis has proposed a nice implementation. He overrides the GetRouteData method and returns the RouteData after some custom code that analyzes the path.

    Regards,
    Tosho Toshev
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
3 posts, 0 answered