How to make a new controller default controller in asp.net core web api

So, I am new in asp.net core, and I am trying to create a new web api in asp.net core. The default controller is Weatherforecast, in other words when I debug the application in the browser the main page is always Weatherforecast. I added a new controller named MainController.cs, and I wrote some simple code there:

[Route("[controller]")]
[ApiController]
public class MainController : ControllerBase
{
    [HttpGet]
    public string getmain()
    {
        return "welcome";
    }

    
}

}
in a word, i want my application’s main controller to be https://localhost:43372/main and not https://localhost:43372/weatherforecast, also I tried to delete Weatherforecast controller and Weatherforecast.cs file, however when I deleted it, the main page was still https://localhost:43372/weatherforecast, but with error because I deleted the files. So, how can i make my new controller default for me web app?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You have to modify launchUrl in the launchSettings.json file that exists in the properties folder.

“launchUrl”: “weatherforecast” -> “launchUrl”: “main”

Please make sure that the main is a valid controller.

Method 2

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

Method 3

I suggest you take a little look at Properties/launchSettings.json.

If you want to create a new controller,
Controller / Right Click / Add / Controller.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x