网页API路由 - 无行动的请求匹配的控制器上找到 [英] Web API Routing - No action was found on the controller that matches the request

查看:827
本文介绍了网页API路由 - 无行动的请求匹配的控制器上找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧网页API路由乐趣。

Ok web api routing fun.

我的Web API航线配置如下。地理编码和信息路由表演很棒。而我困惑,为什么搜索一个不继套件。

my web api route config is below. the geocoding and info routes are performing great. And I'm confused why the search one is not following suite.

/*
    ==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的URL://的WebAPI / API / V1 /搜索/ FC / RV 。此网址产生下面的堆栈跟踪并导致404我认为该公约是,如果你有一个控制器,一个get方法会被默认使用。这似乎是与第一地理编码路线会发生什么。

so i want to have a url like http://webapi/api/v1/search/fc/rv. This url produces the below stack trace and resulting 404. I thought the convention was that if you had a controller with a get method it would be used by default. That seems to be what happens with the first geocoding route.

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 {}版本的第二个地理编码路由/ {控制器} / ** **为/ {要素类} / {returnValues​​} 路由的作品,一切都开心。

Now if I were to make another route more similar to the second geocoding route with the /multiple making the route template api/v{version}/{controller}/**for**/{featureClass}/{returnValues} the routing works and all is happy.

控制器签名

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

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

My hunch is that the async Task part is throwing it off somehow with the default get binding but that doesn't make sense since it works with the modified route.

推荐答案

您请求的URL 的http://的WebAPI / API / V1 /搜索/ FC / RV 是匹配它具有'应力状态和区域变量 v1_Geo codeAPI 路线。由于这些变量名称不匹配的搜索控制器的get方法的参数名称,你看到404,您可以尝试修改你的路由不接受控制器名称作为变量,但有一些像 API / V {版本} /地理位置code / {街道} / {带} API / v {版本} /搜索/ {要素类} / {returnValues​​}

Your request url http://webapi/api/v1/search/fc/rv is matching the v1_GeocodeApi route which has the 'stree' and 'zone' variables. Since these variable names do not match the Search controller's Get method's parameter names, you are seeing 404. You can try modifying your routes to not accept the controller name as variable, but have something like api/v{version}/Geocode/{street}/{zone}, api/v{version}/Search/{featureClass}/{returnValues}

这篇关于网页API路由 - 无行动的请求匹配的控制器上找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆