简体   繁体   English

Web API路由到操作名称

[英]Web API route to action name

I need a controller to return JSON to be consumed by JavaScript so I inherited from the ApiController class but it isn't behaving as I expected. 我需要一个控制器来返回JSON以供JavaScript使用,所以我继承了ApiController类,但它的行为并不像我预期的那样。 The Apress book Pro ASP.NET MVC 4 and most of the online examples I've found give examples like: Apress书籍Pro ASP.NET MVC 4以及我发现的大多数在线示例给出了以下示例:

public class ServicesController : ApiController
{
    public string[] MethodFruit()
    {
        return new string[] { "Apple", "Orange", "Banana" };
}

accessed via the URL: 通过URL访问:

http://mysite/services/methodfruit

But that never works - the resource isn't found. 但这永远不会奏效 - 找不到资源。 The only approach I can get working is to have the controller contain a different method for each HTTP verb, then: 我可以使用的唯一方法是让控制器为每个HTTP动词包含不同的方法,然后:

http://mysite/api/services

Which calls the GET method. 哪个调用GET方法。

I checked the Apress website but they don't seem to have any forums and the current source code is in VS 2012 which I'm not using. 我检查了Apress网站,但他们似乎没有任何论坛,目前的源代码是在VS 2012,我没有使用。 I examined the source files and they seem to think the former approach should work. 我检查了源文件,他们似乎认为前一种方法应该有效。 Is the former approach no longer supported? 是不是再支持前一种方法?

Yep... generally you have to follow the default naming convention expected by ASP.NET WEB API. 是的...通常你必须遵循ASP.NET WEB API期望的默认命名约定。

Check this official doc: 查看此官方文档:

Routing in ASP.NET Web API ASP.NET Web API中的路由

If you do not want to follow the convention, you can try the Routing by Action Name section described in the above linked doc. 如果您不想遵循约定,可以尝试上面链接的doc中描述的“ 按操作名称路由”部分。

Routing by Action Name 按操作名称路由

With the default routing template, Web API uses the HTTP method to select the action. 使用默认路由模板,Web API使用HTTP方法选择操作。 However, you can also create a route where the action name is included in the URI: 但是,您也可以创建一个路径,其中操作名称包含在URI中:

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

In your case, you'd have to do this: 在你的情况下,你必须这样做:

[HttpGet]
public string[] MethodFruit()
{
    return new string[] { "Apple", "Orange", "Banana" };
}

If you want Web API to look for the action name when routing, change the WebApiConfig.cs class in the App_Start folder to this: 如果希望Web API在路由时查找操作名称,请将App_Start文件夹中的WebApiConfig.cs类更改为:

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

Then you can just make a GET request to 然后你可以发出GET请求

http://mysite/api/Services/MethodFruit

Adding additional information on above answer. 添加有关上述答案的其他信息。 Adding RoutePrefix would fix the issue sometimes. 添加RoutePrefix有时会解决问题。 Hope it helps 希望能帮助到你

Controller 调节器

    [RoutePrefix("api/Services")]
    public class ServicesController : ApiController
    {
        [System.Web.Http.AcceptVerbs("GET", "POST")]
        [System.Web.Http.HttpGet]
        [Route("MethodFruit")]
        public string[] MethodFruit()
        {
            return new string[] { "Apple", "Orange", "Banana" };
        }
    }

In config 在配置中

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

i was facing same issue, i found that on my code 我面临同样的问题,我发现我的代码

using System.Web.Mvc; 

i've imported Mvc intead of using System.Web.Http. 我已经使用System.Web.Http导入了Mvc intead。 so it was using Mvc routing not webApi route. 所以它使用Mvc路由而不是webApi路由。 i've replaced that with 我用它替换了

using System.Web.Http 

and it works for me. 它对我有用。 i'm adding my answer so newbies won't repeat the same mistake 我正在添加我的答案,所以新手不会重复同样的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM