[英]404 not found when trying to run Api Controller function from ASP.NET core 5 web app
I am trying to create an API controller in my ASP.NET core 5 Razor Pages web application.我正在尝试在我的 ASP.NET core 5 Razor Pages Web 应用程序中创建一个 API 控制器。
I have created the Api Controller inside a folder named Api
in Visual studio.我在 Visual Studio 中名为
Api
的文件夹中创建了 Api 控制器。
Then when I try to run the following url to test that the api controller works, I get a 404 not found:然后,当我尝试运行以下 url 来测试 api 控制器是否正常工作时,我得到 404 not found:
https://localhost:345345/api/saveimage
What am I missing?我错过了什么?
Api Controller (SaveImageController.cs): Api 控制器 (SaveImageController.cs):
[Route("api/[controller]")]
[ApiController]
public class saveImageController : ControllerBase
{
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "test", "test2" };
}
}
Even though using folder "Api" for your controller will work, it is normal to keep controllers inside a folder called "Controllers".尽管为控制器使用文件夹“Api”会起作用,但将控制器保存在名为“Controllers”的文件夹中是正常的。
Please note that Razor pages are using folder based routing, the same does not apply to controllers.请注意,Razor 页面使用基于文件夹的路由,同样不适用于控制器。
In Startup#ConfigureServices, make sure you have this:在 Startup#ConfigureServices 中,确保你有这个:
services.AddControllers();
In Startup#Configure, make sure you have this:在 Startup#Configure 中,确保你有这个:
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.