【问题标题】:How to make a new controller default controller in asp.net core web api如何在asp.net core web api中创建一个新的控制器默认控制器
【发布时间】:2020-12-29 11:24:29
【问题描述】:

所以,我是 asp.net core 的新手,我正在尝试在 asp.net core 中创建一个新的 web api。默认控制器是 Weatherforecast,换句话说,当我在浏览器中调试应用程序时,主页始终是 Weatherforecast。我添加了一个名为 MainController.cs 的新控制器,并在那里编写了一些简单的代码:

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

    
}

} 总之,我希望我的应用程序的主控制器是 https://localhost:43372/main 而不是 https://localhost:43372/weatherforecast,我也尝试删除 Weatherforecast 控制器和 Weatherforecast.cs 文件,但是当我删除它,主页仍然是 https://localhost:43372/weatherforecast,但是因为我删除了文件而出错。那么,我怎样才能让我的新控制器默认为我的网络应用程序?

【问题讨论】:

    标签: c# asp.net api


    【解决方案1】:

    您必须在属性文件夹中存在的 launchSettings.json 文件中修改 launchUrl。

    "launchUrl": "天气预报" -> "launchUrl": "main"

    请确保主控制器是有效的控制器。

    【讨论】:

    • @givexa 一旦任何回复解决了您的问题,请接受它作为答案,以增加帮助您的人的声誉。
    【解决方案2】:

    我建议你看一下Properties/launchSettings.json。

    如果你想创建一个新的控制器, 控制器/右键单击/添加/控制器。

    【讨论】:

    • 哦,谢谢它的工作!我只需要在 Properties/launchSettings.json 中将 launchurl 从 weatherforecast 更改为 main ....
    • 不客气,有问题可以联系我:)
    【解决方案3】:
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Main}/{action=getmain}/{id?}");
     });
    

    【讨论】:

    • 纯代码答案并不是最有帮助的,即使它们可能会回答问题。请用几行来解释这是如何回答问题的。
    猜你喜欢
    • 2019-06-06
    • 2018-01-10
    • 2018-12-16
    • 1970-01-01
    • 2022-01-27
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    相关资源
    最近更新 更多