Ich habe Standard WebAPI Routing-Konfiguration:WebAPI Routing: api/{Controller}/{id} und api/{Controller}/{Aktion} zugleich
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
);
Ich möchte folgende Szenarien unterstützen:
//api/mycontroller
public IQueryable<MyDTO> Get();
//api/mycontroller/{id} where id can be anything except "customaction1" and "customaction2"
public HttpResponseMessage Get(string id);
//api/mycontroller/customaction
[HttpPost]
public void CustomAction1([FromBody] string data);
[HttpPost]
public void CustomAction2([FromBody] string data);
ich habe versucht, [Route("api/mycontroller/customaction1")]
auf die CustomAction1
Methode anzuwenden, und ähnlich wie CustomAction2
aber immer:
Mehrere Aktionen wurden gefunden, die der Anforderung entsprechen: CustomAction1 auf Typ MyProject.WebApiService.MyController CustomAction2 auf Typ MyProject.WebApiService.MyController
Haben Sie das Attribut Routing konfiguriert? 'config.MapHttpAttributeRoutes()' – Nkosi
Nein. Versucht und funktioniert. Ich werde akzeptieren wie asnwer. Es ist jedoch möglich, das Routing so zu konfigurieren, dass es standardmäßig mit der benutzerdefinierten Aktion ohne die Attribute funktioniert. – Liero
Sie müssten mehr Routen erstellen – Nkosi