skip to Main Content

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.

enter image description here

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


  1. Is this a web api project?

    Check this configuration of yours:

    enter image description here

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

    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

    namespace WebApplication130.Controllers
    {
    
        [ApiController]
        [Route("api/[controller]")]
        public class HomeController : Controller
        {
    
            [Route("test")]
            public string Index()
            {
                return "sucess!";
            }
        }
    }
    

    Result:

    enter image description here

    Login or Signup to reply.
  2. 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:

    dotnet new webapi -f net6.0
    

    and I forgot to include the weatherforecast route on the url:

    https://localhost:{PORT}/weatherforecast
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search