MVC questions
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")
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 );
@Html.MyRouteLink(
"read more"
,
"AgentsRoute"
,
new
controller =
"Agents"
,
action =
"Detail"
,
urlName = item.UrlName
)
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
;
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