网页API路由 - 发现多个动作匹配的要求,即 [英] Web API Routing - multiple actions were found that match the request

查看:329
本文介绍了网页API路由 - 发现多个动作匹配的要求,即的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个路线:

  routes.MapRoute(
产品名称:默认,
网址:{控制器} / {行动} / {ID},
默认设置:新{ID = UrlParameter.Optional}
);

和这个动作:

  [System.Web.Http.HttpPost] 
[System.Web.Http.ActionName(GetLoginSeed)]
公共对象GetLoginSeed()

〔System.Web.Http.HttpPost]
[System.Web.Http.AllowAnonymous]
[System.Web.Http.ActionName(验证)]
公共对象PerformLogin(JObject JR)

这是POST请求:

 的http://本地主机:61971 / API /登录/ GetLoginSeed 

为什么我总是得到一个多个动作被发现符合要求的错误是什么?


解决方案

我得到这个路线:




今天我向您表示是MVC控制器的路线。我希望你明白,网页API控制器是一种完全不同的事情。他们在〜/ App_Start / WebApiConfig.cs 定义自己的路线。



所以一定要确保达已包含在你的Web API路由定义的 {行动} 标记(我重复再次无关,与你的MVC路由定义):

  config.Routes.MapHttpRoute(
的名字: DefaultApi,
routeTemplate:API / {控制器} / {行动}
);


I got this Route:

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { id = UrlParameter.Optional }
        );

And this Actions:

    [System.Web.Http.HttpPost]
    [System.Web.Http.ActionName("GetLoginSeed")]
    public object GetLoginSeed()

    [System.Web.Http.HttpPost]
    [System.Web.Http.AllowAnonymous]
    [System.Web.Http.ActionName("Authenticate")]
    public object PerformLogin(JObject jr)

This is the Post Request:

    http://localhost:61971/api/Login/GetLoginSeed

Why I always get an multiple actions were found that match the request error?

解决方案

I got this Route:

What you have shown is a route for MVC controllers. I hope you realize that Web API controllers are an entirely different thing. They have their own routes defined in the ~/App_Start/WebApiConfig.cs.

So make sure tat you have included the {action} token in your Web API route definition (which I repeat once again has nothing to do with your MVC route definitions):

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}"
);

这篇关于网页API路由 - 发现多个动作匹配的要求,即的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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