c# - ASP.NET MVC Page-URL and Controller/Action Routing -
c# - ASP.NET MVC Page-URL and Controller/Action Routing -
i have problems building asp.net mvc page allows 2 sorts of routing.
i have database pages stored url-path like: /site1/site2/site3 tried utilize irouteconstraint in first route, check wether requested site site database (permalink).
in sec case, want utilize default asp.net mvc {controller}/{action} functionality, providing simple acces *.cshtml.
now don't know if best way. furthermore have problem, how root iroutecontraint.
does have experiance this?
i'm using asp.net mvc 5.
problem solved, final solution:
adding 2 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 , 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; homecoming base.gethttphandler(requestcontext); } }
you can utilize attribute routing in mvc 5 , combine attribute routing convention-based routing check status want on controller class or action methods.
and create constraint utilize on action methods this:
public class valuesconstraint : irouteconstraint { private readonly string[] validoptions; public valuesconstraint(string options) { validoptions = options.split('|'); } public bool match(httpcontextbase httpcontext, route route, string parametername, routevaluedictionary values, routedirection routedirection) { object value; if (values.trygetvalue(parametername, out value) && value != null) { homecoming validoptions.contains(value.tostring(), stringcomparer.ordinalignorecase); } homecoming false; } }
to utilize attribute routing need phone call mapmvcattributeroutes
during configuration , phone call normal convention routing afterwards. should add together constraint before map attributes, code below:
public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); var constraintsresolver = new defaultinlineconstraintresolver(); constraintsresolver.constraintmap.add("values", typeof(valuesconstraint)); routes.mapmvcattributeroutes(constraintsresolver); routes.maproute( name: "default", url: "{controller}/{action}/{id}", defaults: new { controller = "home", action = "index", id = urlparameter.optional } ); }
now on controller class can check route , decide different urls below:
for example: // /mysite/site1 , /mysite/site2 not /mysite/site3
[route("mysite/{site:values(site1|site2)}")] public actionresult show(string site) { homecoming content("from database " + site); }
and kind of checking on controller class well.
i hope gives bit of clue accomplish thing want.
c# asp.net asp.net-mvc asp.net-mvc-routing
Comments
Post a Comment