[英]Web API routing actions
The goal is to allow these four endpoints: 目标是允许这四个端点:
POST v1/invoices<br/>
POST v1/invoices/12345<br/>
POST v1/invoices/12345/attachment<br/>
POST v1/invoices/12345/image
Routing entries: 路由条目:
routes.MapHttpRoute(
name: "InvoiceAttachments",
routeTemplate: "v1/invoices/{id}/attachments",
defaults: new { controller = "invoices", action = "PostAttachment" }
);
routes.MapHttpRoute(
name: "InvoiceImages",
routeTemplate: "v1/invoices/{id}/images",
defaults: new { controller = "invoices", action = "PostImage" }
);
These are my four function definitions in the controller: 这些是我在控制器中的四个函数定义:
[HttpPost]
[ActionName("PostAttachment")]
public HttpResponseMessage PostAttachment(int id)
[HttpPost]
[ActionName("PostImage")]
public HttpResponseMessage PostImage(int id)
[HttpPost]
public HttpResponseMessage Post(int id)
[HttpPost]
public HttpResponseMessage Post()
Yet when I post an invoice using the first URI, the route that gets recognized is the attachments route. 然而,当我使用第一个URI发布发票时,获得识别的路线是附件路线。 How do i have endpoints with different sections after the ID variable?
如何在ID变量后面有不同部分的端点?
Got it! 得到它了!
Route: 路线:
routes.MapHttpRoute(
name: "InvoiceStuff",
routeTemplate: "v1/invoices/{id}/{*action}",
defaults: new { controller = "invoices", action = "" }
);
Function definitions: 功能定义:
[HttpPost]
[ActionName("Attachments")]
public HttpResponseMessage Attachments([FromUri]int id)
[HttpPost]
[ActionName("Images")]
public HttpResponseMessage Images([FromUri]int id)
[HttpPost]
public HttpResponseMessage Post()
[HttpPost]
[ActionName("")]
public HttpResponseMessage Post([FromUri]int id)
Works perfectly. 完美的工作。 Note that in the route I specified a default action of "" because of a defect in webAPI routing with wildcards.
请注意,在路由中我指定了默认操作“”,因为带有通配符的webAPI路由中存在缺陷。 Had i not done the default, a null reference exception would have been thrown.
如果我没有完成默认设置,则会引发空引用异常。 More information on the defect can be found here: http://aspnetwebstack.codeplex.com/workitem/718 - slated to be fixed in ASP.NET v5.
有关缺陷的更多信息,请访问: http : //aspnetwebstack.codeplex.com/workitem/718 - 计划在ASP.NET v5中修复。
Thanks for all your help! 感谢你的帮助!
我认为你必须定义另一个路由,因为第一个URI不应该与InvoiceAttachments路由匹配,因为它最后没有“/ attachments”
Try having a single route 尝试使用单一路线
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}/{action}",
defaults: new { id = RouteParameter.Optional, action = "DefaultAction" }
);
Then decorate functions like 然后装饰像
[HttpPost]
[ActionName("DefaultAction")]
public HttpResponseMessage Post()
[HttpPost]
public HttpResponseMessage PostImage(int id)
Have you tried using constraints rather than defaults? 您是否尝试过使用约束而不是默认值?
routes.MapHttpRoute(
name: "InvoiceAttachments",
routeTemplate: "v1/invoices/{id}/attachments",
defaults: new { controller = "invoices" },
constraints: new { action = "PostAttachment" }
);
routes.MapHttpRoute(
name: "InvoiceImages",
routeTemplate: "v1/invoices/{id}/images",
defaults: new { controller = "invoices" },
constraints: new { action = "PostImage" }
);
routes.MapHttpRoute(
name: "Default",
routeTemplate: "v1/invoices/{id}",
defaults: new { controller = "invoices" }
);
UPDATE: 更新:
You could probably sort of hack it by making the action a url part: 你可以通过将动作作为URL部分来破解它:
routes.MapHttpRoute(
name: "InvoiceAttachments",
routeTemplate: "v1/invoices/{id}/{action}",
defaults: new { controller = "invoices" },
constraints: new {action = "attachments|images"}
);
routes.MapHttpRoute(
name: "default",
routeTemplate: "v1/invoices/{id}",
defaults: new { controller = "invoices", id = RouteParameter.Optional },
constraints: new {action = "attachments|images"}
);
and the controller methods: 和控制器方法:
[HttpPost]
[ActionName("attachments")]
public HttpResponseMessage PostAttachment(int id)
[HttpPost]
[ActionName("images")]
public HttpResponseMessage PostImage(int id)
public HttpResponseMessage Post(int id)
public HttpResponseMessage Post()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.