Routing in ASP.NET -
i need use routing parameters in asp.net application.
public class global : system.web.httpapplication { void application_start(object sender, eventargs e) { registerroutes(); } private void registerroutes() { var routes = routetable.routes; routes.mappageroute( "profile", string.format("{0}/{{{1}}}/", "profile", "id"), "~/views/account/profile.aspx", false, new routevaluedictionary {{"id", null}}); } }
then, navigating "/profile" want on page_load method request.params["id"] null , navigating "/profile/1", request.params["id"] "1".
where made mistake?
with traditional webforms created 2 routes in registerroutes() method.
routes.add("profile", new route("profile", new customroutehandler("~/profile.aspx"))); routes.add("profileid", new route("profile/{id}", new customroutehandler("~/profile.aspx")));
the customroutehandler looked this:
public class customroutehandler : iroutehandler { public customroutehandler(string virtualpath) { this.virtualpath = virtualpath; } public string virtualpath { get; private set; } public ihttphandler gethttphandler(requestcontext requestcontext) { string querystring = ""; httprequest request = httpcontext.current.request; string id = convert.tostring(requestcontext.routedata.values["id"]); if (id.length > 0) { querystring = "?id=" + id; } httpcontext.current.rewritepath( string.concat( virtualpath, querystring)); var page = buildmanager.createinstancefromvirtualpath (virtualpath, typeof(page)) ihttphandler; return page; } }
Comments
Post a Comment