0

嗨,我需要關於web api中的路由和處理程序問題的幫助。我有兩條路線,我希望一條路線使用自定義處理程序,其他路線使用默認路線。我有這個代碼。在web api中使用不同處理程序的路由asp.net

config.Routes.MapHttpRoute(

      name: "NamedActions", 
      routeTemplate: "api/{controller}/{action}/{id}", 
      defaults: new { id = RouteParameter.Optional }, 
      handler: customConfig.MessageHandlers.Add(new BasicHandler(new Repository())) 
     ); 

config.Routes.MapHttpRoute(
      name: "ApiLogin", 
      routeTemplate: "apiLogin/v1/LoginApi", 
      defaults: new { controller = "LoginApi"}//, 
     ); 

但是當我把handler: customConfig.MessageHandlers.Add(new BasicHandler(new Repository()))拋出我一個錯誤(錯誤43「MapHttpRoute」最好的超載沒有一個名爲「處理」參數)。

我想第一條路線使用處理器BasicHandler()Repository(),另一個沒有這個自定義處理程序。這是可能的,有兩個不同的處理程序?

回答

0

我發現我的問題的解決方案,我會錯過的attributte在config.Routes.MapHttpRoute,如果我想把屬性的處理程序,我一定要首先把屬性約束以及後來的處理程序屬性。例如:

config.Routes.MapHttpRoute(

 name: "NamedActions", 
     routeTemplate: "api/{controller}/{action}/{id}", 
     defaults: new { id = RouteParameter.Optional }, 
     constraint: null, 
     handler: customConfig.MessageHandlers.Add(new BasicHandler(new Repository())) 
    ); 
相關問題