首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swagger,使用.net核心3.0开放应用编程接口

Swagger,使用.net核心3.0开放应用编程接口
EN

Stack Overflow用户
提问于 2019-12-27 00:53:36
回答 2查看 2.7K关注 0票数 0

我有一个.net核心3.0应用程序,我正在尝试实现Swashbuckle包。所以我可以做一个http get请求。

我有一个这样的控制器:

代码语言:javascript
复制
  [Route("api/products")]
    [ApiController]
    public class ProductValuesController : Controller
    {
        private DataContext context;

        public ProductValuesController(DataContext data)
        {
            this.context = data;
        }

        [HttpGet("{id}")]
        public Product GetProduct(long id)
        {
            return context.Products.Find(id);
        }
        public IActionResult Index()
        {
            return View();
        }
    }

startup.cs文件如下所示:

代码语言:javascript
复制
public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            string connectionString = Configuration["ConnectionStrings:DefaultConnection"];
            services.AddDbContext<DataContext>(options =>
            options.UseSqlServer(connectionString));

            services.AddControllersWithViews();
            services.AddRazorPages();

            services.AddSwaggerGen(options => {
                options.SwaggerDoc("v1", new OpenApiInfo { Title = "SportsStore", Version = "v1" });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider services)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

            app.UseSwagger();

            app.UseSwaggerUI(options => {
                options.SwaggerEndpoint("/swagger/v1/swagger.json",
                "SportsStore API");
            });

            // SeedData.SeedDatabase(services.GetRequiredService<DataContext>());
        }
    }

但是如果我启动应用程序并浏览到:

代码语言:javascript
复制
https://localhost:5001/swagger/v1/swagger.json

我将看到这个错误:

代码语言:javascript
复制
NotSupportedException: Ambiguous HTTP method for action - ServerApp.Controllers.ProductValuesController.Index (ServerApp). Actions require an explicit HttpMethod binding for Swagger 2.0

所以我的问题是:我必须改变什么,才能让它起作用?

谢谢你。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-27 01:11:53

我也有这个问题,看起来是IActionResult Index()导致了这个问题。你可以像上面提到的那样,用[NonAction]属性来修饰它,然后它就可以修复它了。

票数 1
EN

Stack Overflow用户

发布于 2019-12-27 01:04:18

将控制器中的公共非REST方法装饰为[NoAction]

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59491219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档