简体   繁体   English

尝试从 ASP.NET Core 5 Web 应用程序运行 Api 控制器功能时找不到 404

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

相关问题 覆盖 404 在 Asp.Net 核心 Web API 中找不到 - Override 404 Not found in Asp.Net core Web API 未找到返回 404 的 Asp.Net Core“Web API”路由 - Asp.Net Core "Web API" Routes Returning 404 Not Found 将 asp.net core web api 部署到 aws elastic beanstalk 时出现错误 404 Not Found - Getting Error 404 Not Found when deployed asp.net core web api to aws elastic beanstalk 尝试访问 ASP.NET 核心 Web API 的端点时出现 HTTP 错误 404 - HTTP Error 404 when trying to access endpoint of ASP.NET core Web API 尝试运行ASP.NET Core Web API项目时出现错误NU1605 - Error NU1605 when trying to run ASP.NET Core web API project ASP.NET Core Web Api 控制器端点都返回 404 错误 - ASP.NET Core Web Api controller end points are all returning 404 error Post方法在Asp.net Core 2.2 Web API控制器中返回404 - Post method returns 404 in Asp.net core 2.2 Web API Controller ASP.NET CORE Web API - 删除未触发的操作(未找到404) - ASP.NET CORE Web API - delete action not firing (404 not found) 找不到api / values 404本地主机-ASP.NET Core - api/values 404 not found localhost - ASP.NET Core 从Angular HttpClient到ASP.NET Core 2.2 Web API进行POST调用时出现404错误(启用了CORS) - 404 error when making a POST call from Angular HttpClient to ASP.NET Core 2.2 Web API (with CORS enabled)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM