asp.net mvc 3 - Whats wrong with my routes and actions? -
i asked question based on how create pages based on content table contains following: title , content. followed steps, understanding, in answer given.
i created route so:
public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( "default", // route name "{controller}/{action}/{id}", // url parameters new { controller = "home", action = "index", id = urlparameter.optional } // parameter defaults ); routes.maproute( "contentmanagement", "{title}", new { controller = "contentmanagement", action = "index", title = "{title}" } ); }
i assuming can routes this? can set multiple routes? assuming can pass title to controller action have done?
i created model:
namespace locapp.models { public class contentmanagement { public int id { get; set; } [required] public string title { get; set; } public string content { get; set; } } }
from created controller index action looks such:
public viewresult index(string title) { using (var db = new locappcontext()) { var content = (from c in db.contents c.title == title select c).tolist(); return view(content); } }
so created content title of "bla" when visit site.com/bla error cant find "bla/"
can 1 tell me doing wrong? also, if familiar default layout of asp.net mvc project tabs @ top, create set of tabs lead pages, based on title in database
the main issue when using title, routing engine matching first route , trying find controller title. have implemented similar , found explicitly defining controllers valid default route, processed request appropriately. gave example of controllers allow fit our default route below (home, , error).
you want prevent people giving content same title root level controllers blow pretty well.
public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( "default", "{controller}/{action}/{id}", new {controller = "home", action = "index", id = urlparameter.optional}, new {controller = "home|error|help"}, new[] {"ui_www.controllers"}); routes.maproute( "contentmanagement", "{title}", new {controller = "contentmanagement", action = "index"}); } }