简体   繁体   中英

asp.net webforms dynamic routing

I have a table in sql server with pages and each page created has a slug. What I've been doing is foreach page in the table get the slug and do this:

 routes.MapPageRoute(page.Slug, page.Slug, "~/page.aspx?id=" + page.Id, true, new System.Web.Routing.RouteValueDictionary { { "id", page.Id } });      

so far this works perfectly. My issue is that this runs on global.asax file on application start so if I create a new page that mapping doesn't work unless i restart the website in iis. I was reading this post by haacked but haven't been able to get it to work yet and not sure if this would solve it, cause in theory when creating a page it should refresh automatically all routing..any ideas?

So when using RouteMagic I get the error "Object reference not set to an instance of an object" at this line:

RouteTable.Routes.RegisterRoutes("~/Config/Routes.cs");

You are probably missing dynamic compilation. Check in the sample code where it dynamically compiles the code

var assembly = BuildManager.GetCompiledAssembly("~/Config/Routes.cs");
var registrar = assembly.CreateInstance("Routes") as IRouteRegistrar;

Also, make sure Route.cs is set to Content so it doesn't statically compile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM