Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 3 Routes: Dynamic content from home controller - Where am I going wrong?
    primarykey
    data
    text
    <h2>My Setup</h2> <p>I have a set of controllers in the normal fashion, which have their usual CRUD action methods inside them. Examples of these controllers are Testimonials, Galleries, and FAQs. These have backing models in Entity Framework, such as Testimonial, Gallery and FAQ, respectively.</p> <p>You get to these by this sort of URL: /Galleries/Edit/2 All good so far, and all by default conventions...</p> <p>I also have a set of pages that need to have editable content in them, and these have their content populated from a database via Entity Framework. They use an EF model behind them called "Page". This has a content property (html), and a name property so that I can match the incoming request. These pages are the Home, About and Prices pages.</p> <p>I have chosen the Home controller to do this - I intend to have the index Action work out which Page to load from the DB by a name parameter:</p> <pre><code> [AllowAnonymous] public ActionResult Index(string name = "Home") { // look up the page by name in the DB. var model = context.Pages.FirstOrDefault(p =&gt; p.Title == name); // trap errors. if (model == null) { return RedirectToAction("NotFound", "Error", new { aspxerrorpath = name } ); } // normal path return View(model); } </code></pre> <p>So, I could in theory add new items to the Pages table/DbSet and these would get mapped properly to this controller and action. I will then add an edit action for admin to edit the content that has the same signature as the index action above.</p> <hr> <h2>The Problem</h2> <p>The issue comes with Routing requests...</p> <p>I had 2 initial routes:</p> <pre><code>routes.MapRoute("DynamicAccess", "{name}/{action}", new { controller = "Home", action = "Index" }); routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action="Index", id=UrlParameter.Optional}); </code></pre> <p>This fails when I go to "Galleries/", as it goes through the Home controller each time, and fails if I swap them around. I was also getting requests for Scripts/ folder through to the home controller too....</p> <hr> <h2>My Temporary Solution</h2> <p>My current routes now look like this:</p> <pre><code> routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("favicon.ico"); routes.MapRoute("Gallery", "Gallery/{action}/{id}", new { controller = "Galleries", action = "Index", id = UrlParameter.Optional }); routes.MapRoute("Testimonials", "Testimonials/{action}/{id}", new { controller = "Testimonials", action = "Index", id = UrlParameter.Optional }); routes.MapRoute("FAQs", "FAQs/{action}/{id}", new { controller = "FAQs", action = "Index", id = UrlParameter.Optional }); routes.MapRoute("DynamicAccess", "{name}/{action}", new { controller = "Home", action = "Index" }); routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action="Index", id=UrlParameter.Optional}); routes.MapRoute("Root", "", new { controller = "Home", action = "Index" }); routes.MapRoute("AdminAccess", "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults new { authenticated = new AuthenticatedAdminRouteConstraint() }); </code></pre> <p>You can see here that I've had to declare a route for each of my static pages above the route for the dynamically resolved Home Route.</p> <hr> <h2>Question</h2> <p>This looks clumsy to me - having to add each non-dynamic page to my routes table. Can anyone point me to a cleaner way of doing this please?</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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