asp.net mvc 5 - MVC5 get area name when using attribute routing -
i have old mvc3 site able area of route using following code:
object oarea; routedata.datatokens.trygetvalue("area", out aarea);
i creating new mvc5 application , have started use attribute based routing follows:
[routearea("area")] [routeprefix("test")] public testcontroller { public actionresult index() { return view("index"); } }
unfortunately, appears when use attribute based routing routedata.datatokens collection empty. area information appears buried under routedata in "ms_directroutematches", data follows:
routedata.values["ms_directroutematches"])[0].datatokens.trygetvalue("area", out oarea);
however, wondering if there easier, safer or better way area data in mvc5. area name sub-tool name within larger application, used logic in base controller initialization.
the "safe" way first check existence of ms_directroutematches
, probe area if exists, falling original routedata
object if not.
string area; routedata routedata = httpcontext.request.requestcontext.routedata; if (routedata != null) { if (routedata.values.containskey("ms_directroutematches")) { routedata = ((ienumerable<routedata>)routedata.values["ms_directroutematches"]).first(); } routedata.datatokens.trygetvalue("area", out area); }
Comments
Post a Comment