API回传404

问题描述

我正在创建我的第一个API,我知道我缺少一些东西。每当我从PL层项目发送请求时,都会收到404错误,而从邮递员发送时,则会出现“无法验证第一个证书”错误

public class Startup
{
    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)
    {
        services.AdddistributedMemoryCache();
        services.AddMvc();

        services.AddSession(Options =>
        {
            Options.Cookie.HttpOnly = true;
            Options.Cookie.IsEssential = true;
        });

        services.AddDatabaseServices(Configuration["SomeDB"]);
        services.AddRouting();
        services.AddCORSPolicies();
        services.AddAuthentication();
        services.AddAuthorization();

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1",new OpenApiInfo { Title = "Some API",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)
    {
        app.UseSwagger();
        app.UseSwaggerUI(c => {
            c.SwaggerEndpoint("/swagger/v1/swagger.json","Some API V1");
        });

        app.UseRouting();
        app.UseCors("AllowAll"); //must be after routing and before authorization

        app.UseAuthentication();
        app.UseAuthorization();
        app.UseSession();

        app.UseEndpoints(endpoints => {
            endpoints.MapControllers();
        });
    }
}

这是我的主要方法。在启动和启动连接时,所有这些工作都很好,但是随后什么也无法进行。

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();

        }
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
                webBuilder.UseIISIntegration();
                webBuilder.UseStartup<Startup>();

            });   
    }

我尽可能地简化了方法,因此我不必担心数据传递错误

     [HttpGet]
      public IActionResult EventsGet()
     {
         return Ok();
     }

最后,这是我的jQuery文档就绪函数方法。 api root uri正在从webconfig中拉出,但这就是我得到的。 EventsGet位于API Controllers文件夹的Events文件夹中的EventsController中。

    $.ajax({
        url: "http://localhost:44395/api/Event/EventsGet",type: 'GET',success: function (data) {
            alert('success');
        },error: function () {
            alert('error');
        }
    });

当我使用IIS运行API时,它将加载https:// localhost:44395 / api / values并尝试打开一个显示“找不到localhost网页”的网页。

我最大的问题是我不知道从哪里开始,因为一切都很好,但是什么也没有。我已经进行了一些谷歌搜索,但要么找不到能具体回答我问题的内容,要么我对答案的理解不足以解决问题。

同样在Postman中,我尝试关闭SSL,但是会产生相同的错误。我通读了Swagger文档,并掌握了所有建议。我知道我缺少什么,但是我不确定要查找的内容或如何错误处理API。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)