简体   繁体   English

Asp.Net Core 3.1 Web 应用程序,未找到 Api 页面问题

[英]Asp.Net Core 3.1 Web Application, Api page not found issue

My Environment Windows 10. Visual Studio 2019 Professional, Asp.Net Core 3.1我的环境 Windows 10。Visual Studio 2019 Professional,Asp.Net Core 3.1

I am following a Pluralsight course to teach myself Asp.Net Core 3.1.我正在学习 Pluralsight 课程来自学 Asp.Net Core 3.1。 Following the instructor, I have created the web application.跟随讲师,我创建了 Web 应用程序。 Everything goes well until the instructor adds an api controller to the application.一切顺利,直到讲师向应用程序添加 api 控制器。 It works for him but not for me.这对他有效,但对我无效。

Here's my api controller这是我的 api 控制器

namespace OdeToFood.Api
{
    [Route("api/[controller]")]
    [ApiController]
    public class RestaurantsController : ControllerBase
    {
        private readonly OdeToFoodDbContext _context;

        public RestaurantsController(OdeToFoodDbContext context)
        {
            _context = context;
        }

        // GET: api/Restaurants
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Restaurant>>> GetRestaurants()
        {
            return await _context.Restaurants.ToListAsync();
        }

        // GET: api/Restaurants/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Restaurant>> GetRestaurant(int id)
        {
            var restaurant = await _context.Restaurants.FindAsync(id);

            if (restaurant == null)
            {
                return NotFound();
            }

            return restaurant;
        }

. . . . .

Here's my project structure and hierarchy.这是我的项目结构和层次结构。 在此处输入图片说明

When I rebuild my project, and call the app from local IIS Express, ie https://localhost:44351/ It loads fine.当我重建我的项目,并从本地 IIS Express 调用应用程序时,即 https://localhost:44351/ 它加载正常。 I can interact fully, browse and CRUD entities.我可以完全交互,浏览和 CRUD 实体。 However when I point to any api url, eg https://localhost:44351/api/Restaurants or https://localhost:44351/api/Restaurants/2 I get "This localhost page can't be found".但是,当我指向任何 api url,例如 https://localhost:44351/api/Restaurants 或 https://localhost:44351/api/Restaurants/2 时,我得到“无法找到此本地主机页面”。 The api simply does not load or respond in any way. api 根本不加载或以任何方式响应。

I am familiar with MVC5 where, when creating a new project, in the create project wizard scaffolding, you could check a box to add api functionality.我熟悉 MVC5,其中在创建新项目时,在创建项目向导脚手架中,您可以选中一个框以添加 api 功能。 I am not seeing this in VS2019 for Asp.Net Core 3.1 We Apps.我在 VS2019 for Asp.Net Core 3.1 We Apps 中没有看到这一点。

I promise you have have done my homework before asking this question here.我保证你在这里问这个问题之前已经做完了我的功课。 I have googled to death.我已经谷歌搜索死了。 I have seen MS articles on core 3.1 breaking changes.我看过关于核心 3.1 重大更改的 MS 文章。 I have looked at online project templates.我看过在线项目模板。 I have searched stackoverflow.我已经搜索过stackoverflow。 Maybe my search terms are flawed and I'm simply missing something simple.也许我的搜索词有缺陷,我只是缺少一些简单的东西。

Questions:问题:

  1. Why is the api shown above not loading?为什么上面显示的api没有加载?

  2. Is there a way to add api functionality to an existing Asp.Net Core 3.1 Web Application or do I need to create a separate api project?有没有办法将 api 功能添加到现有的 Asp.Net Core 3.1 Web 应用程序中,还是我需要创建一个单独的 api 项目?

  3. Is there a way to create a new Asp.Net Core 3.1 Web Application with api functionality included?有没有办法创建一个包含 api 功能的新 Asp.Net Core 3.1 Web 应用程序?

My thanks in advance我提前致谢

Kieran基兰

If you'd like to add web APIs feature into an existing Razor Pages project, you need to do additional configuration, like below.如果您想将 Web API 功能添加到现有的 Razor Pages 项目中,您需要进行额外的配置,如下所示。

public void ConfigureServices(IServiceCollection services)
{
    //add services for controllers
    services.AddControllers();

    services.AddRazorPages();

    //...
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    //...

    app.UseRouting();

    //...

    app.UseEndpoints(endpoints =>
    {
        //add endpoints for controller actions
        endpoints.MapControllers();

        endpoints.MapRazorPages();
    });
}

Testing code of controller and action(s)控制器和动作的测试代码

[Route("api/[controller]")]
[ApiController]
public class RestaurantsController : ControllerBase
{
    public IActionResult GetRestaurants()
    {
        return Ok("Restaurants List Here");
    }
}

Test Result测试结果

在此处输入图片说明

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

相关问题 在 ASP.NET Core 3.1 MVC+Razor Pages+Web API 中设置默认页面 - Set default page in ASP.NET Core 3.1 MVC+Razor Pages+Web API ASP.NET Core 3.1:Web API 身份登录 - ASP.NET Core 3.1: Web API identity sign in asp.net core 3.1 web api的解决方案是什么 - what will be the solution with asp.net core 3.1 web api Asp.net 核心 3.1 保护 API 和 web 应用程序 - Asp.net core 3.1 securing API and web app ASP.NET 内核中的全局异常处理程序 Web API 3.1 - Global Exception Handler in ASP.NET Core Web API 3.1 将安全标头添加到 ASP.NET 核心 3.1 Web Api - Adding Security Headers to ASP.NET Core 3.1 Web Api 未处理 Web API 请求 Asp.net Core 3.1 和 Axios - Web API Request not processed Asp.net Core 3.1 and Axios Asp.net Web API .NET Core 3.1 和 Azure AD - system.unauthorizedaccessexception:在不记名令牌中找不到范围或角色声明 - Asp.net Web API .NET Core 3.1 and Azure AD - system.unauthorizedaccessexception: neither scope or roles claim was found in the bearer token ASP.net Core 3.1 web application Calling another asp.net core application url in IFrame resets identity and any other pages redirects to login page - ASP.net Core 3.1 web application Calling another asp.net core application url in IFrame resets identity and any other pages redirects to login page ASP.NET Core 3.1 - 路由问题 - ASP.NET Core 3.1 - routing Issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM