为什么我收到 net::ERR_CONNECTION_RESET 消息?

问题描述 投票:0回答:1

我正在开发 ASP.NET Web Api,我编写了一些代码来测试我的一个 API 控制器。但是当我调用该方法时,chrome 会发送以下内容:

http://localhost:44333/api/GradesReader/getTest/net::ERR_CONNECTION_RESET jquery.min.js:2

这是我的 API 项目 Startup.cs:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }

    


    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(x => x.Filters.Add(new AuthorizeFilter())).AddXmlSerializerFormatters()
                          .AddXmlDataContractSerializerFormatters();

        //For the personal exception handling...
        //services.AddMvc(config =>
        //{
        //    config.Filters.Add(typeof(DiaryExceptionFilter));
        //});

        services.AddDbContext<DiaryDataContext>();
        

        services.AddIdentity<IdentityUser, IdentityRole>()
                .AddEntityFrameworkStores<DiaryDataContext>()
                .AddDefaultTokenProviders();

        services.ConfigureApplicationCookie(opt =>
        {
            opt.Events = new CookieAuthenticationEvents
            {
                OnRedirectToLogin = redirectContext =>
                {
                    redirectContext.HttpContext.Response.StatusCode = 401;
                    return Task.CompletedTask;
                },
                OnRedirectToAccessDenied = redirectContext =>
                {
                    redirectContext.HttpContext.Response.StatusCode = 401;
                    return Task.CompletedTask;
                }
            };
        });

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
        });

        services.AddCors(); //Ezt raktuk bele!
    }
   

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {

        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.UseExceptionHandler(
           options =>
           {
               options.Run(async context =>
               {
                   context.Response.StatusCode = 500;
                   context.Response.ContentType = "application/json";
                    //var ex = context.Features.Get<IExceptionHandlerFeature>();
                    //if (ex != null)
                    //{
                    //    await context.Response.WriteAsync(ex.Error.Message);
                    //}
                });
           });
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

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

        
        //app.UseEndpoints(endpoints =>
        //{
        //    endpoints.MapControllerRoute(
        //        name: "default",
        //        pattern: "{controller=Home}/{action=Index}/{id?}");
        //});
        app.UseEndpoints(endpoints =>                                    
        {
            endpoints.MapControllers();
        });
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });

        app.UseCors(opt => opt.WithOrigins("https://localhost:44333/"));


    }
}

方法如下:

[HttpGet("getTest")]
    public string GetTest()
    {
        return "TEST";
    }

这是 html:

`

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="lib/jquery/dist/jquery.min.js"></script>
    <script src="js/jquery.unobtrusive-ajax.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#myButton").click(function () {
                $.ajax({
                    url: 'http://localhost:44333/api/GradesReader/getTest/',
                    type: 'GET',
                    /*cache: false,*/
                    /*dataType = 'json',*/
                    success: function (result) {
                        console.log(result);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <h1>Test API</h1>

    <input type="button" id="myButton" value="Show Grades" />

</body>
</html>

`

javascript html ajax asp.net-core connection
1个回答
0
投票
url: 'http://localhost:44333/api/GradesReader/getTest/',

在您的应用程序中,您将其配置为使用

https
请求,但上面的请求 URL 是
http
请求。因此,它会显示
net::ERR_CONNECTION_RESET
错误。

要解决此问题,请将 URL 更改为使用

https
:

   url: 'https://localhost:44333/api/GradesReader/getTest/',
© www.soinside.com 2019 - 2024. All rights reserved.