Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the default controller and action in ASP.NET Core API?

I'm creating an ASP.NET Core API app, and currently, and when one creates a new project there is a controller named Values, and by default the API opens it when you run. So, I deleted that controller and added a new controller named Intro, and inside it an action named Get. In the Startup.cs file, I have the following lines of code:

app.UseMvc(opt =>
{
    opt.MapRoute("Default",
        "{controller=Intro}/{action=Get}/{id?}");
});

And my Intro controller looks like this:

[Produces("application/json")]
[Route("api/[controller]")]
[EnableCors("MyCorsPolicy")]
public class IntroController : Controller
{
    private readonly ILogger<IntroController> _logger;

    public IntroController(ILogger<IntroController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public IActionResult Get()
    {
        // Partially removed for brevity
    }
}

But, again when I run the API, it by default tries to navigate to /api/values, but since I deleted the Values controller, now I get a 404 not found error. If I manually then navigate to /api/intro, I get the result that is provided from my Get action inside the Intro controller. How can I make sure that when the API run (for example through Debug->Start Without Debugging) that it by default gets the Get action from the Intro controller?

like image 408
tinker Avatar asked Jul 08 '18 16:07

tinker


People also ask

How would you setup the default route using endpoints?

UseEndpoints(endpoints => { endpoints. MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); Inside the call to UseEndpoints() , we use the MapControllerRoute() method to create a route by giving the name default .

How can we configure conventional routing in ASP.NET Core?

How does routing work in ASP.NET Core MVC? In an empty project, update Startup class to add services and middleware for MVC. Add HomeController to demonstrate the conventional routing (see discussion). Add a controller named WorkController to demonstrate the attribute routing.


2 Answers

You can change it in launchSettings.json file in Properties node. There should be field launchUrl which contains default launching url

like image 103
Alex Riabov Avatar answered Sep 17 '22 16:09

Alex Riabov


With later version of ASP .Net Core, MVC routing is less prominent than it once was, there is general routing now in place which can handle routing of Web APIs, MVC and SignalR amongst other kinds of routes.

If you are using later versions, and not specifically calling app.UseMvc you can do this with the generalised Endpoints configuration:

app.UseEndpoints(endpoints =>
{
   endpoints.MapControllerRoute("default", "{controller=Account}/{action=login}/{id?}");
  // Create routes for Web API and SignalR here too...
});

Where Account and login are your new default controller and actions. These can be MVC or Web API controller methods.

like image 27
Ian Robertson Avatar answered Sep 18 '22 16:09

Ian Robertson