MVC questions

Posted by Community Admin on 04-Aug-2018 04:23

MVC questions

All Replies

Posted by Community Admin on 15-Aug-2012 00:00

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

Posted by Community Admin on 12-Aug-2013 00:00

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: net.tutsplus.com/.../
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.

Posted by Community Admin on 15-Aug-2013 00:00

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

This thread is closed