Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}");
            });
        }
    }
}
like image 606
Hatsumi Avatar asked Feb 25 '18 12:02

Hatsumi


People also ask

How do I change the default URL in Web API?

In order to make ~/api discoverable all you need to do is introduce a new route before the default which handles any calls to ~/api e.g. config. Routes. MapHttpRoute( name: "DiscoverableApi", routeTemplate: "api", defaults: new { controller = "Discoverable", } );

How do I specify a route in Web API?

The default route template for Web API is "api/{controller}/{id}". In this template, "api" is a literal path segment, and {controller} and {id} are placeholder variables. When the Web API framework receives an HTTP request, it tries to match the URI against one of the route templates in the routing table.


2 Answers

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://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app-xplat/

like image 139
Felipe Augusto Avatar answered Sep 28 '22 06:09

Felipe Augusto


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

like image 21
Enrico Avatar answered Sep 28 '22 04:09

Enrico