【问题标题】:Why can't navigate to web api controller为什么无法导航到 web api 控制器
【发布时间】:2014-10-18 12:01:45
【问题描述】:

我第一次使用 Web API 将我的移动应用程序连接到 Web API。
我有 MVC 5 项目并创建了 API 文件夹,并在其中创建了 ProductsController。

namespace DDD.WebUI.API
{
    public class ProductsController : ApiController
    {
        public string GetAllProducts()
        {
            return "Hello From WebAPI";
        }

    }
}

我已经尝试从浏览器访问这个方法:

http://localhost:21879/DDD.WebUI/API/products/GetAllProducts

但我得到 404。
这可能是一些愚蠢的事情,但无法弄清楚:(
更新

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

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

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



        }

更新 2
我有一点进步: 我更新了Application_Start():

GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);

在WebApiConfig 我有:

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

我仍然将 API 控制器存储在 Project/Api/ProductsController 但是现在我可以访问它但仍然无法从中获得价值:

http://localhost:21879/api/products/getallproducts

但我得到的错误是:

<Error>
<Message>The request is invalid.</Message>
<MessageDetail>
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Get(Int32)' in 'GSL.WebUI.Api.ProductsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
</MessageDetail>
</Error>

【问题讨论】:

  • 回答您的更新。如果您以这种方式定义映射,则不应在 url 中使用该操作。试试http://localhost:21879/api/products/,它应该可以工作。然后查看我的答案中的链接以了解一切如何协同工作。

标签: asp.net-mvc asp.net-web-api asp.net-web-api2 asp.net-web-api-routing


【解决方案1】:

我遇到了同样的问题。观察您在 Global.asax.cs 中注册路线的顺序。

这行得通:

GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);

这不是:

RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(WebApiConfig.Register);

【讨论】:

    【解决方案2】:

    我认为查看您的更新,您的操作需要id,它是不可为空的int,而您没有提供它只是将url 修改为http://localhost:21879/API/products/GetAllProducts/2 这里'2' 分配给'id',它将工作。

    在 WebApiConfig 中修改路由为:

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

    只需更改此 Mvc 路由:

        routes. MapRoute(
               name: "DefaultApi" ,
               url: "API/{controller}/{action}/{id}" ,
               defaults: new { controller = "Home" , action = "Index" , id = UrlParameter .Option
           );
    

    有了这个:

         routes. MapRoute(
               name: "DefaultApiMvc" ,
               url: "APIMvc/{controller}/{action}/{id}" ,
               defaults: new { controller = "Home" , action = "Index" , id = UrlParameter .Option
           );
    

    因为否则 Mvc 和 Api 路由会相互冲突...

    【讨论】:

    • 我确实像你说的那样,但又是 404。现在命名空间是 GSL.WebUI.Controllers
    • @1110.. 只需在 GetAllProducts 上设置断点并输入 url localhost:21879/API/products/GetAllProducts 它将命中断点只需检查???
    • 不,它没有进入方法
    • 我做的和你写的一模一样。如果我只添加该路由配置,我将无法访问我的其他控制器。如果我添加像你这样的配置,只是名称不同,我的其他控制器可以工作,但 API 仍然没有。我创建了全新的 ASP MVC 5 项目并将 ProductsController 添加到 Controllers 文件夹并再次相同。
    • 我已经更新了我通过 404 的问题,但现在出现了一些参数错误。但是方法没有任何参数。
    【解决方案3】:

    如果使用那种路由方式,需要为action指定允许的Http方法:

    public class ProductsController : ApiController
    {
        [HttpGet]
        public string GetAllProducts()
        {
            return "Hello From WebAPI";
        }
    }
    

    您可以从此链接获取有关 API 路由的所有信息:http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      相关资源
      最近更新 更多