【问题标题】:How to implement multiple Get methods in WebApi如何在 WebApi 中实现多个 Get 方法
【发布时间】:2017-05-19 16:06:25
【问题描述】:

我在这里尝试实现多个 Get 方法,但浏览器出现错误Multiple actions were found that match the request。为什么会这样?

在 Api Controller 中,我添加了两个方法: 1. GetEmployee 2. HelloDept 如果我注释掉其中一个,它工作正常。

    public class TrailController : ApiController
    {
        private IProduct Repo = new Product();

        [HttpGet]
        public IEnumerable<Employee> GetEmployee()

        {
            var x = Repo.GetEmp();
            return x;

        }
        [HttpGet]
        public IEnumerable<Department> HelloDept()

        {
            var x = Repo.GetDept();
            return x;

          }

RouteConfig.cs

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{api}/{controller}/{action}/{id}",
             //   defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                defaults: new {id = RouteParameter.Optional }


            );  

【问题讨论】:

    标签: asp.net-web-api


    【解决方案1】:

    在 RouteConfig.cs 中更改您的代码。

    发件人

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

    收件人

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

    另外,参考这个-Difference between "MapHttpRoute" and "MapRoute"?

    【讨论】:

      【解决方案2】:

      在您的 WebApiConfig.cs 中更改

      `api/{controller}/{id}` 
      

      `api/{controller}/{action}/{id}`.
      

      然后调用类似的动作 -

      http://localhost:port/api/controller/HelloDept
      http://localhost:port/api/controller/GetEmployee
      
      
      public static class WebApiConfig
      {
          public static void Register(HttpConfiguration config)
          {
              config.MapHttpAttributeRoutes();
      
              config.Routes.MapHttpRoute(
                  name: "DefaultApi",
                  routeTemplate: "api/{controller}/{action}/{id}",
                  defaults: new { id = RouteParameter.Optional }
              );
          }
      }
      

      【讨论】:

      • 我进行了更改,但没有结果相同的错误
      • 找到与请求匹配的多个操作:GetEmployee on type Application.Api.Controllers.TrailController GetById on type Application.Api.Controllers.TrailController HelloDept on type Application.Api.Controllers.TrailController
      • 我在编辑中进行了更改
      • 它应该是api 而不是{api}。还有一件事,您使用的是ApiController,但您发布的是route 用于mvc。您确定您找对地方了吗?它应该是WebAPiConfig.cs 中的HttpRoute
      • 我也改变了即使没有结果...你会来我的 Teamviewr 吗
      猜你喜欢
      • 1970-01-01
      • 2016-11-27
      • 2021-08-15
      • 1970-01-01
      • 2017-05-22
      • 2016-10-03
      • 2021-04-06
      • 1970-01-01
      • 2019-05-29
      相关资源
      最近更新 更多