ASP.Net 核心 "This localhost page can’t be found" HTTP 错误 404

ASP.Net Core "This localhost page can’t be found" HTTP ERROR 404

当我想 运行 我的项目使用 .Net Core MVC 架构和 Visual Studio 2019 程序在我的 Mac 上,我收到错误消息“这个本地主机页面不能成立”。我正在共享 Startup.cs 和控制器 类。

我正在使用 .NetCore 3.1 版。

提前致谢。

namespace Test
{
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.AddControllers();
        services.AddSingleton<VendorRegistrationService>();
        services.AddCors(o => o.AddPolicy("ReactPolicy", builder =>
        {
            builder.AllowAnyOrigin()
                   .AllowAnyMethod()
                   .AllowAnyHeader();
            //.AllowCredentials();
        }));
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseCors("ReactPolicy");

        app.UseAuthorization();

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

VendorRegistrationController.cs

 namespace Test.Controllers
{

[Produces("application/json")]
[Route("api/[controller]")]
[ApiController]
[EnableCors("ReactPolicy")]

public class VendorRegistrationController : ControllerBase
{
    public readonly VendorRegistrationService vendorRegistrationService;

    public VendorRegistrationController(VendorRegistrationService vendorRegistrationService)
    {
        this.vendorRegistrationService = vendorRegistrationService;
    }

    [HttpPost]
    public IActionResult Post([FromBody] VendorRegistration vendorRegistration)
    {
        return CreatedAtAction("Get", vendorRegistrationService.Create(vendorRegistration));
     }
  }
 }

这是网络 api 项目吗?

检查你的这个配置:

"profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/home/test",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },

如果你的launchUrl没有给默认的url或者给错路由,就会出现上面的错误,找不到默认的启动路径。您可以添加您需要的内容,例如:

控制器

namespace WebApplication130.Controllers
{

    [ApiController]
    [Route("api/[controller]")]
    public class HomeController : Controller
    {

        [Route("test")]
        public string Index()
        {
            return "sucess!";
        }
    }
}

结果: