简体   繁体   中英

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?

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM