动态更改 ASP.NET MVC 路由

发布于 2024-08-26 12:44:51 字数 490 浏览 12 评论 0原文

通常,当我查看 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小帐篷 2024-09-02 12:44:51

我会考虑实现我自己的 IRouteHandler 并将一些自定义逻辑放入我的自定义 ControllerActionInvoker 中。它将如何运作?路由表不会动态更改,但您可以在自定义 ControllerActionInvoker 中检查路由路径中的随机参数,并调用或不调用相应的操作。

我的路线:

routes.Add 
( 
    new Route 
        ( 
            "blog/comment/{*data}", 
            new RouteValueDictionary(new {controller = "blog", action = "comment", data = ""}), 
            new MyRouteHandler() 
        ) 
); 

我的路线处理程序:

    class MyRouteHandler : IRouteHandler 
{ 

public IHttpHandler GetHttpHandler(RequestContext requestContext) 
    { 
        return new MyHttpHandler(requestContext); 
    } 
}`

我的处理程序:

class MyHttpHandler : MvcHandler 
{ 
    public MyHttpHandler(RequestContext requestContext) : base(requestContext) 
    { 
    } 

    protected override void ProcessRequest(HttpContextBase httpContext) 
    { 
        IController controller = new BlogController(); 
        (controller as Controller).ActionInvoker = new MyActionInvoker(); 
        controller.Execute(RequestContext); 
    } }`

以及我的操作调用程序,其中应该对是否处理操作的自定义逻辑进行编码:

    class MyActionInvoker : ControllerActionInvoker 
{ 
    protected override ActionResult InvokeActionMethod(MethodInfo methodInfo, IDictionary<string, object> parameters) 
    { 

        var data = ControllerContext.RouteData.GetRequiredString("data"); 


 // put my custom logic to check whetever I'll handle the action or not. The data could be a parameter in the database for that purpose.

        return base.InvokeActionMethod(methodInfo, parameters); 
    } 
} 

我不知道这是最好的解决方案,但目前这是我想到的解决方案。

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 :

routes.Add 
( 
    new Route 
        ( 
            "blog/comment/{*data}", 
            new RouteValueDictionary(new {controller = "blog", action = "comment", data = ""}), 
            new MyRouteHandler() 
        ) 
); 

My I route handler :

    class MyRouteHandler : IRouteHandler 
{ 

public IHttpHandler GetHttpHandler(RequestContext requestContext) 
    { 
        return new MyHttpHandler(requestContext); 
    } 
}`

My handler :

class MyHttpHandler : MvcHandler 
{ 
    public MyHttpHandler(RequestContext requestContext) : base(requestContext) 
    { 
    } 

    protected override void ProcessRequest(HttpContextBase httpContext) 
    { 
        IController controller = new BlogController(); 
        (controller as Controller).ActionInvoker = new MyActionInvoker(); 
        controller.Execute(RequestContext); 
    } }`

and my action ivoker where the custom logic for handling an action or not should be coded :

    class MyActionInvoker : ControllerActionInvoker 
{ 
    protected override ActionResult InvokeActionMethod(MethodInfo methodInfo, IDictionary<string, object> parameters) 
    { 

        var data = ControllerContext.RouteData.GetRequiredString("data"); 


 // put my custom logic to check whetever I'll handle the action or not. The data could be a parameter in the database for that purpose.

        return base.InvokeActionMethod(methodInfo, parameters); 
    } 
} 

I don't know it it's the best solution but for now it's the one that comes to my mind.

海之角 2024-09-02 12:44:51

考虑到实际问题背景,通常的做法是包含动态创建的交易号。它应该存储在隐藏的表单字段以及服务器端会话字典中,并且仅对一个请求有效。

我认为现在很多框架都提供了这样的安全机制;而这种攻击类型称为跨站点请求伪造 (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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文