I have problems building an ASP.NET MVC page which allows two sorts of routing.
I have a database where all pages are stored with an url-path like: /Site1/Site2/Site3 i tried to use an IRouteConstraint in my first route, to check wether the requested site is a site from my database (permalink).
In the second case, i want to use the default asp.net mvc {controller}/{action} functionality, for providing simple acces from an *.cshtml.
Now i don't know if this is the best way. Furthermore i have the problem, how to root with the IRouteContraint.
Does anyone have any experiance with this?
I'm using asp.net mvc 5.
Problem solved, final solution:
Adding this two routes:
routes.MapRoute( "FriendlyUrlRoute", "{*FriendlyUrl}" ).RouteHandler = new FriendlyUrlRouteHandler(); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Page", action = "Load", id = UrlParameter.Optional }, namespaces: controllerNamespaces.ToArray() );My own Route-Handler:
public class FriendlyUrlRouteHandler : System.Web.Mvc.MvcRouteHandler { protected override IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext) { var friendlyUrl = (string)requestContext.RouteData.Values["FriendlyUrl"]; WebPageObject page = null; if (!string.IsNullOrEmpty(friendlyUrl)) { page = PageManager.Singleton.GetPage(friendlyUrl); } if (page == null) { page = PageManager.Singleton.GetStartPage(); } // Request valid Controller and Action-Name string controllerName = String.IsNullOrEmpty(page.ControllerName) ? "Page" : page.ControllerName; string actionName = String.IsNullOrEmpty(page.ActionName) ? "Load" : page.ActionName; requestContext.RouteData.Values["controller"] = controllerName; requestContext.RouteData.Values["action"] = actionName; requestContext.RouteData.Values["id"] = page; return base.GetHttpHandler(requestContext); } }