动态更改 ASP.NET MVC 路由
通常,当我查看 ASP.Net MVC 应用程序时,路由表会在启动时进行配置,并且此后就不会再被触及。
我对此有几个问题,但它们彼此密切相关:
- 是否可以在运行时更改路由表?
- 我将/应该如何避免线程问题?
- 是否有更好的方法来提供动态 URL?我知道 ID 等可以出现在 URL 中,但不知道这如何适用于我想要实现的目标。
- 如何避免这种情况,即使我定义了默认控制器/操作路由,该默认路由也不适用于特定组合,例如“评论”控制器上的“发布”操作不可通过默认路由使用?
背景:垃圾评论发送者通常会从网站获取发布 URL,然后不再费心浏览该网站来进行自动垃圾邮件发送。如果我经常将我的帖子 URL 修改为某个随机 URL,垃圾邮件发送者将不得不返回该网站并找到正确的帖子 URL 来尝试发送垃圾邮件。如果该 URL 不断变化,我认为这可能会使垃圾邮件发送者的工作变得更加乏味,这通常意味着他们放弃受影响的 URL。
usually, when I look at a ASP.Net MVC application, the Route table gets configured at startup and is not touched ever after.
I have a couple of questions on that but they are closely related to each other:
- Is it possible to change the route table at runtime?
- How would/should I avoid threading issues?
- Is there maybe a better way to provide a dynamic URL? I know that IDs etc. can appear in the URL but can't see how this could be applicable in what I want to achieve.
- How can I avoid that, even though I have the default controller/action route defined, that default route doesn't work for a specific combination, e.g. the "Post" action on the "Comments" controller is not available through the default route?
Background: Comment Spammers usually grab the posting URL from the website and then don't bother to go through the website anymore to do their automated spamming. If I regularly modify my post URL to some random one, spammers would have to go back to the site and find the correct post URL to try spamming. If that URL changes constantly I'd think that that could make the spammers' work more tedious, which should usually mean that they give up on the affected URL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会考虑实现我自己的 IRouteHandler 并将一些自定义逻辑放入我的自定义 ControllerActionInvoker 中。它将如何运作?路由表不会动态更改,但您可以在自定义 ControllerActionInvoker 中检查路由路径中的随机参数,并调用或不调用相应的操作。
我的路线:
我的路线处理程序:
我的处理程序:
以及我的操作调用程序,其中应该对是否处理操作的自定义逻辑进行编码:
我不知道这是最好的解决方案,但目前这是我想到的解决方案。
I would consider to implement my own IRouteHandler and put some custom logic in my custom ControllerActionInvoker. How it would work ? The route table wouldn't dynamically change but you could check in your custom ControllerActionInvoker for a random parameter in the route path and invoke or not the corresponding action.
My route :
My I route handler :
My handler :
and my action ivoker where the custom logic for handling an action or not should be coded :
I don't know it it's the best solution but for now it's the one that comes to my mind.
考虑到实际问题背景,通常的做法是包含动态创建的交易号。它应该存储在隐藏的表单字段以及服务器端会话字典中,并且仅对一个请求有效。
我认为现在很多框架都提供了这样的安全机制;而这种攻击类型称为跨站点请求伪造 (csrf)。
Considering the actual problem background, the usual approach is to include a dynamically created transaction number. It should be stored in a hidden form field as well as in the server side session dictionary and only be valid for exactly one request.
I think today a lot of frameworks provide such a security mechanism; whereas this attack type is known as Cross-Site-Request-Forgery (csrf).