如何使用HTTP谓词在HttpConfiguration类中进行路由而不是显式操作名称?

时间:2017-09-13 13:24:40

标签: c# asp.net asp.net-mvc-routing httpconfiguration

我有一个基本的asp.net自我主机应用程序,它使用HttpConfiguration类并包含以下路径:

config.Routes.MapHttpRoute(
    name: "login",
    routeTemplate: "login",
    defaults: new { controller = "Login", action = "Get" }
);

我想根据请求中包含的HTTP谓词定义操作,而不是在URL中指定。

curl http://domain/login -X GET

应该调用Get类上的LoginController方法。并且

curl http://domain/login -X POST -H 'Content-Length: 1' -d 'x'

应致电帖子'发布'控制器类中的方法。如果我将路线配置调整到

,我已经能够测试发布请求是否有效
config.Routes.MapHttpRoute(
    name: "login",
    routeTemplate: "login/{action}",
    defaults: new { controller = "Login", action = "Get" }
);

然后curl到:

curl http://domain/login/post -X POST -H 'Content-Length: 1' -d 'x'

但这是在http请求和URI中指定post动词(我不想这样做)。

=====编辑

列维的回答:https://stackoverflow.com/a/2143991/3114742解决了我的问题

0 个答案:

没有答案