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}");
});
}
}
}
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", } );
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.
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/
If you mean the page that should open when you click run:
Right click on your project -> properties -> Debug -> Launch browser
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With