Hatsumi
Hatsumi

Reputation: 2228

How to define initial url on web api project?

I created a project on Visual Studio Code on Mac and wanted to set my default page. I wrote the code on the "Startup.cs", but it's not working.

When I run the project and open the browser it shows that is not finding the controller.

Follow the Startup.cs code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace ProblemsV4
{
    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.AddMvc();
        }

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

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Problem}/{action=Index}");
            });
        }
    }
}

Upvotes: 7

Views: 2966

Answers (2)

Enrico
Enrico

Reputation: 3454

If you mean the page that should open when you click run:

Right click on your project -> properties -> Debug -> Launch browser enter image description here

Upvotes: 3

Felipe Augusto
Felipe Augusto

Reputation: 8184

You should create a MVC project to be able to define the initial URL.

Your problem is because you're creating a Web API project instead of a MVC project, because it's an API, doesn't make sense to have a default initial url, like MVC project (because of the views),

You could follow this tutorial to create a MVC application and set the default controller: https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app-xplat/

Upvotes: 3

Related Questions