【问题标题】:Cannot solve Multiple operations with path 'Entry' and method 'GET' with Attribute routing无法通过属性路由解决路径“Entry”和方法“GET”的多个操作
【发布时间】:2018-09-28 20:45:30
【问题描述】:

我正在托管自己的 WebApi,如下所示: 这是作为 Windows 服务运行的,因此可以启动和停止。

我正在使用 Swagger/Swashbuckle 来测试我的 api。

我有以下代码。

public class ApiShell : IApiShell
    {
        IDisposable _webApp;

        public void Start()
        {
            _webApp = WebApp.Start<Startup>("http://localhost:9090");
            Console.WriteLine($"Web server running at 'http://localhost:9090'");
        }

        public void Stop()
        {
            _webApp.Dispose();
        }

        internal class Startup
        {
            //Configure Web API for Self-Host
            public void Configuration(IAppBuilder app)
            {
                var config = new HttpConfiguration();
                config.EnableSwagger(c =>
                {
                    c.SingleApiVersion("1", "Api");
                    c.PrettyPrint();
                }).EnableSwaggerUi(c => c.EnableDiscoveryUrlSelector());

                var resolver = new AutofacWebApiDependencyResolver(IoC.Container);
                config.DependencyResolver = resolver;

                config.Routes.MapHttpRoute("DefaultApi", "{controller}/{id}", new { id = RouteParameter.Optional });

                config.MapHttpAttributeRoutes(); //Swagger does not load when I have the following uncommented
                config.Formatters.Remove(config.Formatters.XmlFormatter);

                app.UseWebApi(config);

                GlobalConfiguration.Configuration.EnsureInitialized();
            }
        }
    }

问题是,当我不添加config.MapHttpAttributeroutes 时,出现以下错误:

具有路径“Entry”和方法“GET”的多个操作

当我添加 config.MapHttpAttributeRoutes 来尝试解决这个问题时,我收到以下错误:

对象尚未初始化。确保在所有其他初始化代码之后在应用程序的启动代码中调用 HttpConfiguration.EnsureInitialized()

我添加了GlobalConfiguration.Configuration.EnsureInitialized();,但这没有帮助。

这是我的路线:

[RoutePrefix("api")]
    public class EntryController : ApiController
    {
        private IActorSystemShell _actorSystem;

        public EntryController(IActorSystemShell actorSystem)
        {
            _actorSystem = actorSystem;
        }

        [HttpPost]
        public async Task<dynamic> AddPhoneBook([FromBody] Entry entry)
        {
            ...
        }

        [HttpGet]
        public async Task<dynamic> GetEntries()
        {
            ...
        }

        [HttpGet]
        [Route("id/{id}")]
        public async Task<dynamic> GetEntryById(int id)
        {
            ...
        }

        [HttpGet]
        [Route("text/{searchText}/SearchAll")]
        public async Task<dynamic> SearchEntries(string searchText)
        {
            ...
        }

        [HttpGet]
        [Route("book/{bookId}/{searchText}")]
        public async Task<dynamic> SearchInPhoneBook(int bookId, string searchText)
        {
            ...
        }

    }

【问题讨论】:

  • 能否将答案标记为已接受或提供更多信息
  • 我稍后会在这里发布我的答案。

标签: c# visual-studio asp.net-web-api url-routing


【解决方案1】:

这可能是由于 herehere 所讨论的 Swagger 限制。您可以尝试为所有 HttpGet 调用指定一个特定名称:

[SwaggerOperation("GETOPERATION")]

这也可能是因为并非所有操作都具有路由属性

【讨论】:

  • 是的,我最好的选择是路由或 SwaggerOperation 声明
猜你喜欢
  • 1970-01-01
  • 2016-09-22
  • 2011-06-08
  • 2018-04-05
  • 2017-04-19
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
相关资源
最近更新 更多