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.
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());
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
2) below is my controller
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.
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
There are occasions where you might want to use custom routing without using route hijacking.
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 theRenderModel
.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.And the corresponding route.
Hi James,
Thanks for reply.
i will update my code as above.
Regards
Mehul Gajjar.
No worries :)
Let me know how you get on.
is working on a reply...