[英]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.