2013-05-30 110 views
2

好的web api路由的乐趣。Web API路由 - 在控制器上找不到匹配请求的动作

我的web api路由配置如下。地理编码和信息路线表现出色。我很困惑为什么搜索不跟随套件。

/* 
    ==Geocoding Endpoints== 
*/ 

config.Routes.MapHttpRoute(
    name: "v1_GeocodeApi", 
    routeTemplate: "api/v{version}/{controller}/{street}/{zone}", 
    defaults: new 
    { 
     action = "get", 
     controller = "Geocode", 
     version = "1" 
    }); 

config.Routes.MapHttpRoute(
    name: "v1_GeocodeMultipleApi", 
    routeTemplate: "api/v{version}/{controller}/multiple", 
    defaults: new 
    { 
     action = "multiple", 
     controller = "Geocode", 
     version = "1" 
    }); 

/* 
    ==Search Endpoints== 
*/ 

config.Routes.MapHttpRoute(
    name: "v1_SearchApi", 
    routeTemplate: "api/v{version}/{controller}/{featureClass}/{returnValues}", 
    defaults: new 
    { 
     action = "Get", 
     controller = "Search", 
     version = "1" 
    }); 

/* 
    ==Info Endpoints== 
*/ 

config.Routes.MapHttpRoute(
    name: "v1_InfoApi", 
    routeTemplate: "api/v{version}/{controller}/FeatureClassNames", 
    defaults: new 
    { 
     action = "FeatureClassNames", 
     controller = "Info", 
     version = "1" 
    }); 
} 

所以我想有一个像http://webapi/api/v1/search/fc/rv这样的网址。这个url产生了下面的堆栈跟踪,结果是404.我认为约定是,如果你有一个带get方法的控制器,它将被默认使用。这似乎是第一个地理编码路线发生的情况。

w3wp.exe Information: 0 : Request, Method=GET, 
Url=http://webapi/api/v1/Search/fc/rv, 
Message='http://webapi/api/v1/Search/fc/rv' 
w3wp.exe Information: 0 : Message='Search', 
Operation=RouteVersionedControllerSelector.SelectController w3wp.exe 
Information: 0 : 
Message='WebAPI.API.Controllers.API.Version1.SearchController', 
Operation=DefaultHttpControllerActivator.Create w3wp.exe Information: 
0 : Message='WebAPI.API.Controllers.API.Version1.SearchController', 
Operation=HttpControllerDescriptor.CreateController w3wp.exe 
Information: 0 : Message='Will use same 'JsonpMediaTypeFormatter' 
formatter', 
Operation=JsonpMediaTypeFormatter.GetPerRequestFormatterInstance 
w3wp.exe Information: 0 : Message='Selected 
formatter='JsonpMediaTypeFormatter', content-type='application/json; 
charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate w3wp.exe 
Warning: 0 : Message='UserMessage='No HTTP resource was found that 
matches the request URI 
'http://webapi/api/v1/Search/fc/rv'.', 
MessageDetail='No action was found on the controller 'Search' that 
matches the request.'', 
Operation=ApiControllerActionSelector.SelectAction, Status=404 
(NotFound), Exception=System.Web.Http.HttpResponseException: 
Processing of the HTTP request resulted in an exception. Please see 
the HTTP response returned by the 'Response' property of this 
exception for details. at 
System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext 
controllerContext) at 
System.Web.Http.Tracing.Tracers.HttpActionSelectorTracer.<>c__DisplayClass2.<System.Web.Http.Controllers.IHttpActionSelector.SelectAction>b__0() at 
System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter 
traceWriter, HttpRequestMessage request, String category, TraceLevel 
level, String operatorName, String operationName, Action`1 beginTrace, 
Action execute, Action`1 endTrace, Action`1 errorTrace) w3wp.exe 
Warning: 0 : Message='UserMessage='No HTTP resource was found that 
matches the request URI 
'http://webapi/api/v1/Search/fc/rv'.', 
MessageDetail='No action was found on the controller 'Search' that 
matches the request.'', Operation=SearchController.ExecuteAsync, 
Status=404 (NotFound), 
Exception=System.Web.Http.HttpResponseException: Processing of the 
HTTP request resulted in an exception. Please see the HTTP response 
returned by the 'Response' property of this exception for details. 
at 
System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext 
controllerContext) at 
System.Web.Http.Tracing.Tracers.HttpActionSelectorTracer.<>c__DisplayClass2.<System.Web.Http.Controllers.IHttpActionSelector.SelectAction>b__0() at 
System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter 
traceWriter, HttpRequestMessage request, String category, TraceLevel 
level, String operatorName, String operationName, Action`1 beginTrace, 
Action execute, Action`1 endTrace, Action`1 errorTrace) at 
System.Web.Http.Tracing.Tracers.HttpActionSelectorTracer.System.Web.Http.Controllers.IHttpActionSelector.SelectAction(HttpControllerContext 
controllerContext) at 
System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext 
controllerContext, CancellationToken cancellationToken) at 
System.Web.Http.Tracing.Tracers.HttpControllerTracer.<>c__DisplayClass4.<System.Web.Http.Controllers.IHttpController.ExecuteAsync>b__0() 
at 
System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEndAsync[TResult](ITraceWriter 
traceWriter, HttpRequestMessage request, String category, TraceLevel 
level, String operatorName, String operationName, Action`1 beginTrace, 
Func`1 execute, Action`2 endTrace, Action`1 errorTrace) w3wp.exe 
Information: 0 : Response, Status=404 (NotFound), Method=GET, 
Url=http://webapi/api/v1/Search/fc/rv, 
Message='Content-type='application/json; charset=utf-8', 
content-length=unknown' 

现在,如果我再拍路线更类似于与/多使得路径模板api/v{version}/{controller}/**for**/{featureClass}/{returnValues}路由工程第二地理编码路线,一切都是幸福的。

控制器签名

public class SearchController : ApiController 
{ 
    [HttpGet] 
    public async Task<HttpResponseMessage> Get(string featureClass, string returnValues, [FromUri] SearchOptions options) 
    { 
     // ... 
    } 
} 

我的直觉是,异步任务部分与默认莫名其妙地把它扔关闭GET绑定但这没有任何意义,因为其与修正路线的作品。

回答

1

您的请求url http://webapi/api/v1/search/fc/rv与匹配具有'stree'和'zone'变量的v1_GeocodeApi路由。由于这些变量名称与搜索控制器的Get方法的参数名称不匹配,因此您会看到404.您可以尝试修改路由以不接受控制器名称作为变量,但有类似于api/v{version}/Geocode/{street}/{zone},api/v{version}/Search/{featureClass}/{returnValues}

+0

我会说这是因为堆栈跟踪表示它选择了WebAPI.API.Controllers.API.Version1.SearchController。但修改路由以删除{conroller}确实解决了这个问题。 – Steve

+0

我有点不清楚你的评论......这是不正确的吗? –

+0

我在编辑之前写过。我不认为v1_geocodeapi路由被选中是因为堆栈跟踪中的文本。但现在有意义,因为当参数名称不匹配时,通常会引发404。这正是发生的情况。我希望跟踪会显示选择的路线。我认为还有其他项目可以做到这一点。 – Steve

相关问题