当前位置: 首页 > 知识库问答 >
问题:

使用.NET Core3.0的开放api

欧阳昊阳
2023-03-14

我有一个。NET Core3.0应用程序,我正在尝试实现Swashbuckle包。所以我可以执行http get请求。

我有一个这样的控制器:

  [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();
        }
    }

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>());
        }
    }

https://localhost:5001/swagger/v1/swagger.json
NotSupportedException: Ambiguous HTTP method for action - ServerApp.Controllers.ProductValuesController.Index (ServerApp). Actions require an explicit HttpMethod binding for Swagger 2.0

Thnak你。

共有1个答案

充栋
2023-03-14

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

 类似资料:
  • 问题内容: 创建函数saveTxtIndividualTracks(track,folder,i)基于python3.4.3和Windows 7: 使用功能: 运行代码时,出现以下错误: FileNotFoundError:[错误2]没有这样的文件或目录:’E:/phoneTracks/TA92903URN7ff/0.txt’ 我在E中创建了文件夹。我对mode的功能感到困惑,该模式用于创建新文件

  • 本文向大家介绍iOS9开放的新API--Spotlight使用指南,包括了iOS9开放的新API--Spotlight使用指南的使用技巧和注意事项,需要的朋友参考一下 1.Spotloight是什么?   Spotlight在iOS9上做了一些新的改进, 也就是开放了一些新的API, 通过Core Spotlight Framework你可以在你的app中集成Spotlight。集成Spotlig

  • 淘宝开放平台(Taobao Open Platform,简称TOP)提供了丰富的API,开发者可以方便通过QAP调用TOP API。 单个API QN.top.invoke({ query: { method: 'taobao.user.seller.get', // TOP 接口名称 fields: 'nick,sex' // 除了`method`字段外,其他字段为请求的

  • 我正在尝试用配置我的,以便对用户进行身份验证。我已经配置了openldap,它在中运行良好。但是对于,它给出的错误是 以下是配置:

  • 问题内容: 是否有人可以通过BookSleeve库获取Redis的固定模式? 我的意思是: BookSleeve的作者@MarcGravell 建议不要每次都打开和关闭连接,而是在整个应用程序中保持一个连接。但是如何处理网络中断?也就是说,连接可能首先成功打开,但是当某些代码尝试对Redis进行读/写操作时,连接可能已断开,您必须重新打开它(如果连接无法打开,则正常失败- 但是完全可以满足您的设计

  • 问题内容: 如何将打开的二进制流(Python 2 ,Python 3和an)包装在中? 我正在尝试编写将保持不变的代码: 在Python 2上运行。 在Python 3上运行。 使用从标准库生成的二进制流(即,我无法控制它们是什么类型) 将二进制流设为测试双倍(即没有文件句柄,无法重新打开)。 产生一个包装指定流的。 之所以需要,是因为标准库的其他部分需要使用它的API。存在其他类似文件的类型,