2016-11-10 2 views
0

Ich bin WCF-Dienst erneut implementieren, und ich verwende WebAPI 2.2 + OData v4. Problem, mit dem ich konfrontiert bin, ist, dass ich Route haben muss, die "_" enthält und ich kann es nicht implementieren. Zur Zeit habe ich dies:So ändern Sie die Route in WebApi + Odata-Projekt

public class AnnotationSharedWithController : ODataController 
{ 
    ... 
    [EnableQuery] 
    public IQueryable<AnnotationSharedWith> Get() 
    { 
     return _unitOfWork.AnnotationSharedWith.Get(); 
    } 
    ... 
} 

und meine WebApiConfig.cs sieht wie folgt aus:

public static void Register(HttpConfiguration config) 
{ 
    config.MapODataServiceRoute("webservice",null,GenerateEdmModel()); 
     config.Count(); 
} 

private static IEdmModel GenerateEdmModel() 
{ 
    var builder = new ODataConventionModelBuilder(); 
    builder.EntitySet<AnnotationSharedWith>("annotation_shared_with"); 
     return builder.GetEdmModel(); 
} 

wenn ich ausgeben Anfrage ich Fehler folgende receive

{ "message": " Es wurde keine HTTP-Ressource gefunden, die mit der Anforderungs-URI 'http://localhost:12854/annotation_shared_with' übereinstimmt. "," MessageDetail ": " Es wurde kein Typ gefunden, der mit dem Controller mit der Bezeichnungübereinstimmt'annotation_shared_with'. " }

Antwort

0

Ihr Routing verwenden könnte führt dies zu erreichen:

  1. Mit ODataRouteAttribute Klasse:

    public class AnnotationSharedWithController : ODataController 
    { 
        [EnableQuery] 
        [ODataRouteAttribute("annotation_shared_with")] 
        public IQueryable<AnnotationSharedWith> Get() 
        { 
         //your code 
        } 
    } 
    
  2. Mit ODataRoutePrefixAttribute und ODataRouteAttribute Klassen:

    [ODataRoutePrefixAttribute("annotation_shared_with")] 
    public class AnnotationSharedWithController : ODataController 
    { 
        [EnableQuery] 
        [ODataRouteAttribute("")] 
        public IQueryable<AnnotationSharedWith> Get() 
        { 
         //your code 
        } 
    } 
    
0

Standardmäßig sucht OData nach annotation_shared_withController, wie in Ihrem EDM-Modell definiert. Da Ihr Controller den Namen AnnotationSharedWithController hat, wird 404 zurückgegeben.

Wenn Sie Ihre Controller-Klasse neu installieren, wird das Problem gelöst. Aber Sie werden mit unordentlichen Klassennamen enden.

Sie können Ihre eigenen Routing-Konventionen finden Routing Conventions in ASP.NET Web API 2 Odata für mehr Details

Hoffe, es hilft implementieren.

Verwandte Themen