WebAPI ActionName路由工作一半 [英] WebAPI ActionName routing half working

查看:129
本文介绍了WebAPI ActionName路由工作一半的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在构建WebAPI,尝试使用ActionName路由到正确的方法.它可以与我尝试调用的一种方法一起使用,但另一种方法会出现404错误.

我的WebAPI配置文件:

public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

我的WebAPI Controller方法的格式如下:

第一个是工作的:

[ActionName("postdb")]
public IEnumerable<string[]> postDB(string id)
{ ...

第二个没有:

[ActionName("getquery")]
public IEnumerable<string[]> getQuery(string tables)
{ ...

我从角度调用它们的方式相同(Temp是作为参数传递的字符串):

$http.post('api/Test/postdb/' + temp).then(function (response) { ...

$http.get('api/Test/getquery/' + temp).then(function (response) { ...

我尝试过更改两个动作的名称,第一个动作与名称无关,第二个则与名称无关.我还尝试过重新排序它们,在GET和POST之间更改,以及更改参数.

有什么建议吗?

解决方案

不确定为什么要使用ActionName设置路由?

您可能应该查看Route属性.例如.

[HttpPost]
[Route("postdb")]
// Action doesn't have to be called 'postdb'
public IEnumerable<string[]> postDB(string id)

ActionName通常用于其他目的( ActionName的目的)

尽管如此,我认为您的示例中发生了一些奇怪的事情-我认为设置ActionName不应影响那里的路由.要进行调试,建议您设置失败请求跟踪",以查看请求在何时无法完成操作.

这些是WebAPI中选择操作的基本规则(

  • 如果以上都不是,则该方法支持POST.

  • 因此,在您的示例中,postdb方法可能映射到POST方法.但是可能是,因为它是小写的,但ASP.NET不喜欢并应用了规则3-如果您确实要使用ActionName,请尝试使用ActionName("PostDB")[ActionName("GetQuery")](无论出于何种原因) ),而不是Route.

    I have been building a WebAPI, trying to route to the right methods with ActionName. It works with one of my methods I try to call, but the other one gets a 404 error.

    My WebAPI Config file:

    public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
    
            // Web API routes
            config.MapHttpAttributeRoutes();
    
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    

    My WebAPI Controller methods are formatted as such:

    This first one is the working one:

    [ActionName("postdb")]
    public IEnumerable<string[]> postDB(string id)
    { ...
    

    This second one does not:

    [ActionName("getquery")]
    public IEnumerable<string[]> getQuery(string tables)
    { ...
    

    I'm calling both of them the same way from angular (Temp is a string that is being passed as the argument):

    $http.post('api/Test/postdb/' + temp).then(function (response) { ...
    

    and

    $http.get('api/Test/getquery/' + temp).then(function (response) { ...
    

    I have tried changing names of both actions, the first one works no matter the name, the second one doesn't work no matter the name. I have also tried reordering them, changing between GET and POST, and changing arguments.

    Any suggestions?

    解决方案

    Not sure why you are using ActionName to setup routing?

    You should probably be looking at Route attribute. eg.

    [HttpPost]
    [Route("postdb")]
    // Action doesn't have to be called 'postdb'
    public IEnumerable<string[]> postDB(string id)
    

    ActionName is usually used for a different purpose (Purpose of ActionName)

    Nevertheless, I think something odd is going on in your example - I'd think setting ActionName shouldn't have affected routing there. To debug I'd suggest to set up Failed Request Tracing to see at which point the request fails to reach the action.

    These are the basic rules for Action selection in WebAPI (http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection)

    1. You can specify the HTTP method with an attribute: AcceptVerbs, HttpDelete, HttpGet, HttpHead, HttpOptions, HttpPatch, HttpPost, or HttpPut.

    2. Otherwise, if the name of the controller method starts with "Get", "Post", "Put", "Delete", "Head", "Options", or "Patch", then by convention the action supports that HTTP method.

    3. If none of the above, the method supports POST.

    So, in your example postdb method may map to the POST method. But may be because it's in lower case ASP.NET didn't like that and applied Rule 3 - try with ActionName("PostDB") and [ActionName("GetQuery")] if you really want to use ActionName (for whatever reason) instead of Route.

    这篇关于WebAPI ActionName路由工作一半的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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