简体   繁体   English

asp.net Webforms动态路由

[英]asp.net webforms dynamic routing

I have a table in sql server with pages and each page created has a slug. 我在sql server中有一个带有页面的表格,创建的每个页面都有一个段。 What I've been doing is foreach page in the table get the slug and do this: 我一直在做的是在表中的foreach页上获取该信息并执行以下操作:

 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. 我的问题是,这会在应用程序启动时在global.asax文件上运行,因此,如果我创建一个新页面,则除非在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? 我正在读这篇文章的haacked ,但一直没能得到它的却并不能确定工作,如果创建一个页面就会自动刷新所有routing..any的想法时,这将解决这个问题,在理论上原因在哪里?

So when using RouteMagic I get the error "Object reference not set to an instance of an object" at this line: 因此,当使用RouteMagic时,在此行出现错误“对象引用未设置为对象的实例”:

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. 另外,请确保将Route.cs设置为Content,以便它不会静态编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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