繁体   English   中英

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

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

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

[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删除它,主页仍然是 https://localhost:43372/weatherforecast,但是因为我删除了文件而出错。 那么,我怎样才能让我的新 controller 默认为我 web 应用程序?

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

“launchUrl”:“天气预报” ->“launchUrl”:“主要”

请确保 main 是有效的 controller。

我建议您看一下Properties/launchSettings.json

如果要新建controller、 Controller/右键/添加/Controller。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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