要更改ASP.Net Web API的名称,您需要进行以下步骤:
打开项目中的Startup.cs
文件。
在ConfigureServices
方法中,找到以下代码行:
services.AddControllers();
services.AddControllers().AddApplicationPart(typeof(YourNewControllerName).Assembly);
这将使Web API能够加载除weatherforecast
之外的其他控制器。
打开项目中的Controllers
文件夹,并创建一个新的控制器类,如YourNewControllerName.cs
。
在YourNewControllerName.cs
文件中,编写您的自定义控制器代码。例如:
using Microsoft.AspNetCore.Mvc;
[Route("api/[controller]")]
[ApiController]
public class YourNewControllerName : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return Ok("This is your new controller.");
}
}
现在,您可以通过访问https://localhost:xxxx/api/yournewcontrollername
来访问您的新控制器。