C# Web API 路由到操作名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18661946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:52:53  来源:igfitidea点击:

Web API route to action name

c#asp.net-web-apiasp.net-web-api-routing

提问by Dave

I need a controller to return JSON to be consumed by JavaScript so I inherited from the ApiControllerclass but it isn't behaving as I expected. The Apress book Pro ASP.NET MVC 4 and most of the online examples I've found give examples like:

我需要一个控制器来返回 JSON 以供 JavaScript 使用,因此我从ApiController该类继承了它,但它的行为并不符合我的预期。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. I examined the source files and they seem to think the former approach should work. Is the former approach no longer supported?

我查看了 Apress 网站,但他们似乎没有任何论坛,当前的源代码在 VS 2012 中,我没有使用。我检查了源文件,他们似乎认为前一种方法应该有效。是否不再支持前一种方法?

采纳答案by Leniel Maccaferri

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 Namesection described in the above linked doc.

如果您不想遵循约定,您可以尝试上面链接的文档中描述的按操作名称路由部分。

Routing by Action Name

With the default routing template, Web API uses the HTTP method to select the action. However, you can also create a route where the action name is included in the URI:

按动作名称路由

使用默认路由模板,Web API 使用 HTTP 方法来选择操作。但是,您也可以创建一个路由,其中​​操作名称包含在 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" };
}

回答by Mahesh

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

然后你可以向

http://mysite/api/Services/MethodFruit

回答by Praveen M P

Adding additional information on above answer. Adding RoutePrefixwould fix the issue sometimes. Hope it helps

Controller

添加有关上述答案的其他信息。添加RoutePrefix有时会解决这个问题。希望它可以帮助

控制器

    [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 } );

回答by Uttam Ughareja

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. so it was using Mvc routing not webApi route. i've replaced that with

我已经导入了 Mvc 而不是使用 System.Web.Http。所以它使用的是 Mvc 路由而不是 webApi 路由。我已经用

using System.Web.Http 

and it works for me. i'm adding my answer so newbies won't repeat the same mistake

它对我有用。我正在添加我的答案,所以新手不会重复同样的错误