Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom MVC routing based on URL stored in database
    primarykey
    data
    text
    <p>I'm trying to add some custom routing logic based on url's stored in a database for mvc. (CMS Like), I think its fairly basic, but I feel like i'm not really getting anywhere.</p> <p>Basically a user may type url's such as:</p> <pre><code>www.somesite.com/categorya/categoryb/categoryf/someitem www.somesite.com/about/someinfo </code></pre> <p>In the database these items are stored, along with the type they are, i.e. a normal page, or a product page.</p> <p>Depending on this I then want to actually hit a different 'action' method, i.e. I would like the above to hit the methods:</p> <pre><code>PageController/Product PageController/Normal </code></pre> <p>These actions then load the content for this page and display the same view (product view, or a normal view).</p> <p>Using the normal way of routing won't work, since I could potentially have things like;</p> <pre><code>cata/producta cata/catb/catc/catd/cate/catf/producta </code></pre> <p>Now i've been looking here : <a href="https://stackoverflow.com/questions/379558/mvc-net-routing#379823">ASP.NET MVC custom routing for search</a></p> <p>And trying to use this as a basis, but how do I actually 'change' my action method I want to hit within the InvokeActionMethod call?</p> <p>Using MVC 3.0 btw.</p> <p>Thanks for any help/suggestions</p> <p><strong>Final Solution:</strong></p> <p>Global.asax</p> <pre><code>routes.MapRoute( "Default", "{*path}", new { controller = "Page", action = "NotFound", path= "Home" } ).RouteHandler = new ApplicationRouteHandler(); </code></pre> <p>Route Handlers</p> <pre><code>public class ApplicationRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { return new ApplicationHandler(requestContext); } } public class ApplicationHandler : MvcHandler, IRequiresSessionState { public ApplicationHandler(RequestContext requestContext) : base(requestContext) { } protected override IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state) { var url = RequestContext.RouteData.Values["path"].ToString(); var page = SomePageService.GetPageByUrl(url); if (page == null) { RequestContext.RouteData.Values["Action"] = "NotFound"; } else { RequestContext.RouteData.Values["Action"] = page.Action; RequestContext.RouteData.Values["page"] = page; } return base.BeginProcessRequest(httpContext, callback, state); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload