locked
How to route to an endpoint from "/" route? RRS feed

  • Question

  • User1846752346 posted

    Hi, I have created a new ASP.NET Core (5.0) Web API project. When I run the project from command line, I get 404 not found error since there are no redirects configured. I have created a HomeController, with the route as api/Home. I want to redirect to the default Index method in HomeController when I run from command line. 

    I tried adding:

        app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });



    But it didn't redirect.

    I finally ended up writing:

        app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                    endpoints.MapGet("/", async context =>
                    {
                        await context.Response.WriteAsync("Hello World!");
                    });
                });



    I also tried editing launchSettings.json and added api/Home in launchUrl, even this didn't work. When I manually try to go to https://localhost:5001/api/home, it works.</div> <div>Is there a way using which I can route to a controller's method when "/" route is hit?

    Sunday, March 7, 2021 4:00 PM

All replies

  • User475983607 posted

    Hi, I have created a new ASP.NET Core (5.0) Web API project.

    Web API does not have a UI.  The default Web API template displays a swagger documentation page.  Can you explain the design intent?  Perhaps you want MVC not Web API?

    Sunday, March 7, 2021 4:12 PM
  • User1846752346 posted

    yes you are right, apis don't need a home page or anything but something is better than 404. The swagger endpoint is also not working as a default route when set from launchSettings in launchUrl: "swagger". When navigated manually, it works.

    Monday, March 8, 2021 4:50 AM