目标是允许这四个端点:
POST v1/invoices<br/>
POST v1/invoices/12345<br/>
POST v1/invoices/12345/attachment<br/>
POST v1/invoices/12345/image
路由条目:
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" }
);
这些是我在控制器中的四个函数定义:
[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()
然而,当我使用第一个URI发布发票时,获得识别的路线是附件路线。如何在ID变量后面有不同部分的端点?
答案 0 :(得分:7)
知道了!
路线:
routes.MapHttpRoute(
name: "InvoiceStuff",
routeTemplate: "v1/invoices/{id}/{*action}",
defaults: new { controller = "invoices", action = "" }
);
功能定义:
[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)
完美无缺。请注意,在路由中我指定了默认操作“”,因为带有通配符的webAPI路由中存在缺陷。如果我没有完成默认设置,则会引发空引用异常。有关缺陷的更多信息可以在这里找到:http://aspnetwebstack.codeplex.com/workitem/718 - 计划在ASP.NET v5中修复。
感谢您的帮助!
答案 1 :(得分:1)
我认为你必须定义另一个路由,因为第一个URI不应该与InvoiceAttachments路由匹配,因为它在末尾没有“/ attachments”
答案 2 :(得分:0)
尝试使用单一路线
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}/{action}",
defaults: new { id = RouteParameter.Optional, action = "DefaultAction" }
);
然后装饰像
这样的功能[HttpPost]
[ActionName("DefaultAction")]
public HttpResponseMessage Post()
[HttpPost]
public HttpResponseMessage PostImage(int id)
答案 3 :(得分:0)
您是否尝试使用约束而不是默认值?
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" }
);
<强>更新强>
你可能通过将动作作为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"}
);
和控制器方法:
[HttpPost]
[ActionName("attachments")]
public HttpResponseMessage PostAttachment(int id)
[HttpPost]
[ActionName("images")]
public HttpResponseMessage PostImage(int id)
public HttpResponseMessage Post(int id)
public HttpResponseMessage Post()