【问题标题】:ASP.NET web API not showing actionsASP.NET Web API 不显示操作
【发布时间】:2020-01-24 02:45:38
【问题描述】:

我有一个使用实体框架创建的 ASP.NET Web api。我在控制器的文件夹中添加了我的控制器,它为我生成了 API 代码。但是当我运行应用程序时它不显示我添加的控制器,它只显示应用程序附带的默认 API 页面,如下所示。

This is what it displays

我希望它看起来像这样

Expected Output

【问题讨论】:

  • 你希望看到什么?
  • 嗨@matias-cicero 我希望在那里看到我的控制器以及我的控制器中可用的所有REST调用。例如 GET : api/contracts/{id}
  • 你的控制器是否继承自 ApiController?
  • 参见this questiondocs。并向我们​​展示控制器代码。
  • 嗨,selepe,请分享WebApiConfig类,

标签: c# asp.net-mvc rest asp.net-web-api


【解决方案1】:
public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

            // Web API routes
            config.MapHttpAttributeRoutes();

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

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

注意:在 WebApiConfig.cs 文件中添加“ActionApi”路由

【讨论】:

  • 虽然此代码 sn-p 可能会解决问题并提供一些有限的即时帮助。 proper explanation 将通过展示为什么这是解决问题的好方法,并使其对有其他类似问题的未来读者更有用,从而大大提高其长期价值。请考虑edit您的回答以添加一些解释,包括您所做的假设。
猜你喜欢
  • 1970-01-01
  • 2018-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
  • 2014-02-04
  • 2015-05-16
  • 2013-06-15
  • 1970-01-01
相关资源
最近更新 更多