When I want to run my project with .Net Core MVC architecture with Visual Studio 2019 program on my Mac, I get the error "This localhost page can’t be found". I am sharing Startup.cs and controller classes.
I am working with .NetCore version 3.1.
Thanks in advance.
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));
}
}
}
2
Answers
Is this a web api project?
Check this configuration of yours:
If your launchUrl is not given a default url or given a wrong route, the above error will occur, and the default launch path cannot be found. You can add what you need, such as:
Controller
Result:
In my case I was running the sample project at:
https://learn.microsoft.com/en-us/training/modules/build-web-api-aspnet-core/3-exercise-create-web-api
using:
and I forgot to include the weatherforecast route on the url: