Bu hata yeni bir MVC Api dosyası oluşturduktan sonra her ne kadar farklı isimlere sahip olsada mağlesef MVC Controller mantığındaki gibi {controller}/{action}/{id} şeklinde bir route’a sahip olmadığından hata veriyor bu hatanın çözümüne basit bir şekilde ulaşabilirsiniz.
MVC Hata: Web API Routing – multiple actions were found that match the request
Hatanın çözümünden önce sorun şundan kaynaklanıyor ilk defa mvc projenize api controller ekliyorsanız App_Start klasörü altına WebApiConfig adından bir sınıf dosyası açılıyor burada defaul olarak gelen routeTemplate şu şekilde
C#(WebApiConfig.cs)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | using System.Web.Http; namespace ApiController { public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type. // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries. // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712. //config.EnableQuerySupport(); } } } |
Üstteki kodda belki farketmişsinizdir standart bir mvc projesi açtığınızda mvc projenize ait Route işlemlerinin yer aldığı RouteConfig adlı sınıfınız içerisindeki şablonda routeTemplate daha farklı aslında farkı controller’dan sonra action placeholder’ininda yer alması diyebiliriz. Şimdi RouteConfig.cs dosyamıza gözatalım.
C#(RouteConfig.cs)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System.Web.Mvc; using System.Web.Routing; namespace ApiController { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } } |
Gördüğünüz gibi api öneki yok ve action placeholder’i var aslında bizim api sınıfımız action placeholder’ina sahip olmadığından direk olarak isme göre bakmayıp parametre’ye göre baktığından WebApi patlıyor bu sorunu aşmak için yapmanız gereken WebApiConfig sınıfımızdaki routeTemplate action placeholder’ini eklemek olacaktır.
C#(WebApiConfig.cs)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | using System.Web.Http; namespace Genotıp.IdentitySave { public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type. // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries. // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712. //config.EnableQuerySupport(); } } } |
adamsın hacı çıldırcaktım az daha 😀
Teşekkür ederim 🙂