ASP.Net Core“找不到此本地主机页面”HTTP ERROR 404

Posted

技术标签:

【中文标题】ASP.Net Core“找不到此本地主机页面”HTTP ERROR 404【英文标题】:ASP.Net Core "This localhost page can’t be found" HTTP ERROR 404 【发布时间】:2021-12-07 04:09:19 【问题描述】:

当我想在我的 Mac 上使用 .Net Core MVC 架构和 Visual Studio 2019 程序运行我的项目时,我收到错误“找不到此 localhost 页面”。我正在共享 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));
     
  
 

【问题讨论】:

【参考方案1】:

这是一个 web 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!";
        
    

结果:

【讨论】:

以上是关于ASP.Net Core“找不到此本地主机页面”HTTP ERROR 404的主要内容,如果未能解决你的问题,请参考以下文章

在 ASP.NET Core 5.0 Web API 中实现 DelegatingHandler?

在 ASP.NET Core 中使用 Serilog 使用 Fluentd 将日志写入 Elasticsearch

ASP.NET Core 将 http 重定向到 https

ASP.NET Core WebApi Jwt 基于角色的授权不起作用

Asp.Net Core 7 preview 4 重磅新特性--限流中间件

强制在 ASP.NET Core 中具有查询参数的所有 Url