简体   繁体   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

So, I am new in asp.net core, and I am trying to create a new web api in asp.net core.所以,我是 asp.net 核心的新人,我正在尝试在 Z18465E732F5E724 核心中创建一个新的 web api。 The default controller is Weatherforecast, in other words when I debug the application in the browser the main page is always Weatherforecast.默认的 controller 是 Weatherforecast,换句话说,当我在浏览器中调试应用程序时,主页始终是 Weatherforecast。 I added a new controller named MainController.cs, and I wrote some simple code there:我添加了一个名为 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 deleted it, the main page was still https://localhost:43372/weatherforecast, but with error because I deleted the files. } 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,但是因为我删除了文件而出错。 So, how can i make my new controller default for me web app?那么,我怎样才能让我的新 controller 默认为我 web 应用程序?

You have to modify launchUrl in the launchSettings.json file that exists in the properties folder.您必须在属性文件夹中的launchSettings.json文件中修改 launchUrl。

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

Please make sure that the main is a valid controller.请确保 main 是有效的 controller。

I suggest you take a little look at Properties/launchSettings.json .我建议您看一下Properties/launchSettings.json

If you want to create a new controller, Controller / Right Click / Add / Controller.如果要新建controller、 Controller/右键/添加/Controller。

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

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

相关问题 如何在ASP.Net core 2.1 Web API中查看“默认控制器”页面? - How can I make a view of a Controller as Default page in ASP.Net core 2.1 Web API? 如何更改 ASP.NET Core API 中的默认控制器和操作? - How to change the default controller and action in ASP.NET Core API? 如何从控制器进行 HTTP 调用? 使用 Web API 的 Asp.Net Core C# - How to make HTTP call from Controller ? to Use web API's Asp.Net Core C# ASP.NET 核心 Web API 与实体框架核心默认脚手架 Z594C103F2C6E04C3DAZ8AB05E 混淆 put? - ASP.NET Core Web API with Entity Framework Core default scaffolded controller confuses put and post? 具有通用参数的 ASP.NET Core Web API 控制器 - ASP.NET Core Web API controller with generic params 无法在 ASP.NET 6 核 Web ZDB974238714CA8DE634A7CE1D0 中搭建 controller 脚手架 - Not able to scaffold a controller in ASP.NET 6 Core Web API GraphQL ASP.NET Core Web API Controller 自省问题 - Problem with introspection of GraphQL ASP.NET Core Web API Controller 具有许多参数的ASP.Net Core Web API Controller操作 - ASP.Net Core Web API Controller action with many parameters Asp.Net MVC 中 Web API 的默认控制器操作 - Default controller action for Web API in Asp.Net MVC 如何为ASP.NET Core中的Controller Action覆盖默认反序列化? - How to override default deserialization for Controller Action in ASP.NET Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM