如何在控制器中添加路线
我想在将新对象提交到数据库后映射一条新路线。因此,例如,如果我输入名称为“Test”的对象,我希望立即有一条新路由来解析“Test.aspx”。
我
System.Web.Routing.RouteTable.Routes.MapRoute(obj.NameUrl, obj.NameUrl + extension, new { controller = "per", action = "Index", name = obj.NameUrl });
在控制器中尝试过,但它不起作用(没有错误,只是生命周期中的时间可能不正确?)。相同的代码适用于 Application_Start()
I'd like to map a new route after I commit a new object to db. So for example if i enter object with name "Test" I would like to have a new route immediately, to resolve "Test.aspx".
I tried
System.Web.Routing.RouteTable.Routes.MapRoute(obj.NameUrl, obj.NameUrl + extension, new { controller = "per", action = "Index", name = obj.NameUrl });
in controller but it does not work (no error, just probably not right time in life cycle?). Same code works in Application_Start()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该避免动态注册路由。
Application_Start
中的以下静态路由应该能够处理具有动态路由参数的场景:并且如果扩展也必须是动态的:
然后您可以使用 Index 操作来处理请求此路线:
如果您想生成此操作的链接:
You should avoid registering routes dynamically. The following static route in your
Application_Start
should be able to handle your scenario of having dynamic route parameters:and if the extension has to be dynamic as well:
and then you could have the Index action to handle requests to this route:
and if you want to generate a link to this action: