ASP.NET 中的 HTTP 控制器不会发布/获取任何请求

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

我一直在尝试在本地使用curl在ASP.NET(使用.NET 8)上测试连接到API控制器,但是尽管所有代码似乎都是正确的,但我没有收到任何发送到服务器的请求:

这是

InternController.cs
代码:

using backendApp.Data;
using backendApp.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace backendApp.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class InternController : ControllerBase
    {
        private readonly IWebHostEnvironment _environment;
        private readonly DataContext _context;
        private readonly ILogger _logger;

        public InternController(IWebHostEnvironment environment, DataContext context)
        {
            _environment = environment;
            _context = context;
            _logger = LoggerFactory.Create(builder => builder.AddConsole()).CreateLogger<InternController>();
        }

        // ...

        [HttpGet("listthemall")]
        public async Task<IActionResult> ListAllInterns()
        {
            _logger.LogInformation("Listing all interns...");
            return Ok("Hello Bunch of Interns");
        }

    }
}

这是我的

Program.cs
上的代码:

using backendApp.Data;
using backendApp.Middleware;
using backendApp.Services;
using backendApp.Services.Interfaces;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Azure;
using SoapCore;
using SoapCore.Extensibility;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddDbContext<DataContext>(options =>
{ 
    options.UseMySql(builder.Configuration.GetConnectionString("DefaultConnection"),
    ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("DefaultConnection")));
    options.LogTo(Console.WriteLine, LogLevel.None);
});

builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.SetMinimumLevel(LogLevel.Debug);

builder.Services.AddSoapCore();
builder.Services.AddScoped<IAccountService, AccountService>();
builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<IInternUtilityService, InternUtilityService>();
builder.Services.AddScoped<IFaultExceptionTransformer, FaultExceptionTransformer>();

builder.Logging.ClearProviders();
builder.Logging.AddConsole();


builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowAll",
        builder =>
        {
            builder.AllowAnyOrigin()
                   .AllowAnyMethod()
                   .AllowAnyHeader();
        });
});


builder.Services.AddHttpClient("NoSSLValidation")
    .ConfigurePrimaryHttpMessageHandler(() =>
    {
        var handler = new HttpClientHandler();
        handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
        return handler;
    });


var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseExceptionHandler("/error");
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCors("AllowAll");
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

   
// UseSoaps Configurations...

app.Urls.Add("http://*:5879");
//App Runner
app.Run();

注意: 我在 Windows 10 上使用 Visual Studio Community 2022,并使用防火墙上该特定端口的所有允许规则(UDP 和 TCP)

我一直在使用命令

curl -X GET http://localhost:5879/api/Intern/listthemall
来测试对
ListAllInterns()
上的
InternController.cs
的任何请求,我希望在其中接收字符串:
Hello Bunch of Interns

c# asp.net-core curl visual-studio-2022 .net-8.0
1个回答
0
投票

注释掉下面这行代码,curl 就可以工作了。

//app.UseHttpsRedirection();
© www.soinside.com 2019 - 2024. All rights reserved.