【问题标题】:MVC: Route Get / Post to different controllers. How?MVC:路由获取/发布到不同的控制器。如何?
【发布时间】:2011-07-10 18:07:36
【问题描述】:

我正在编写一个 MVC 控制器,我需要同时处理数据返回以及长轮询“数据已更改”,例如来自 SAME (!) url 的行为。对此我无能为力 - 我正在为已经存在的应用程序实现代理,因此我无法对 API 进行任何扩展/修改。

我的主要问题是: * POST 操作必须立即完成。 * GET 操作需要更长的时间(有时可能需要几个小时)。

我可以以某种方式重写两者以转到不同的控制器吗?另一种方法是……嗯……使两者都异步,只是 POST 正在完成右三,然后。

有人对此发表评论吗?

【问题讨论】:

    标签: asp.net-mvc-3 asp.net-routing


    【解决方案1】:

    您应该能够在路由级别使用约束来控制 url 转到哪个控制器/操作。

    routes.MapRoute(
        "route that matches only GETs for your url",
        "your url",
        new { controller = "some controller", action = "some action" },
        new { httpMethod = new HttpMethodConstraint("GET") }
    );
    
    routes.MapRoute(
       "route that matches only POSTs for your url",
       "your url",
        new { controller = "some other controller", action = "some other action" },
        new { httpMethod = new HttpMethodConstraint("POST") }
    );
    

    【讨论】:

    • 非常感谢 ;) 这就是我所缺少的 ;)
    • 如果每行分别以“name:”、“url:”、“defaults:”为前缀 => 你在“new { httpMethod...”行的前缀是什么?
    • @full_prog_full 前缀只是方法声明中的参数名称,在本例中为constraints
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 2019-03-28
    相关资源
    最近更新 更多