简体   繁体   English

Asp.net core - 类库上的 Web Api

[英]Asp.net core - Web Api on Class Library

I am trying to make n-tier application where web api is kept on a different class library.我正在尝试制作 n 层应用程序,其中 web api 保存在不同的类库中。 I made a TestController controller class in a different class library and my code goes like this我在不同的类库中创建了一个TestController控制器类,我的代码是这样的

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace PkrUni.SMS.Api
{
    [ApiController]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class TestController : ControllerBase
    {
        public IEnumerable<string> Get()
        {
            return new string[] { "Chris", "Hadfield" };
        }
    }
}

Now my question is how can I access this web api on my main project.现在我的问题是如何在我的主项目中访问这个 web api。 I had already added a reference to that class library on main project but it doesn't working.我已经在主项目中添加了对该类库的引用,但它不起作用。 My api is not working.我的 api 不起作用。 404 Page Not Found error shows up while trying to access the api.尝试访问 api 时出现404 Page Not Found错误。 This is my project structure.这是我的项目结构。

在此处输入图片说明

What am I doing wrong please help me.我做错了什么请帮助我。

Try to install Microsoft.AspNetCore.Mvc package in the razor class library.尝试在 razor 类库中安装Microsoft.AspNetCore.Mvc包。 And in startup.cs use:在 startup.cs 中使用:

services.AddMvc().AddApplicationPart(Assembly.Load(new AssemblyName("PkrUni.SMS.Area.Api")));

Refer to here .请参阅此处

I test your scenario by creating a asp.net core2.2 MVC project and razor class library without any problem.我通过创建一个 asp.net core2.2 MVC 项目和 razor 类库来测试你的场景,没有任何问题。 Below is my structure:下面是我的结构:

在此处输入图片说明

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

相关问题 将ASP.Net Core 2(.NET Framework)Web Api拆分为类库和托管项目是否可行/明智? - Is it possible/sensible to split ASP.Net Core 2 (.NET Framework) Web Api into Class Library and Hosting projects? Razor class library empty image - ASP.NET Core 6 Web API - Razor class library empty image - ASP.NET Core 6 Web API 在 ASP.NET 核心 Web ZDB974238714CA8DE634A7CE1D0FZA8 的 class 库项目中使用 AutoMapper - Use AutoMapper inside a class library project in ASP.NET Core Web API 如何解决 ASP.NET Core Web API 项目中类库的 Windsor 容器依赖项? - How to resolve Windsor container dependencies for a class library in ASP.NET Core Web API project? asp.net core web api 2中的抽象类模型绑定 - Abstract class model binding in asp.net core web api 2 回顾 UML class ASP.NET 内核图 Web ZDB974238714CA8DE634A7CE1D083A - Review of UML class Diagram of a ASP.NET Core Web API ASP.NET Web API帮助页面链接到类库 - ASP.NET Web API Help Page Link to a Class Library 使用 ASP.NET Core 2.2 Web API 项目中的实体框架类库时抛出异常 - Exception thrown when using Entity Framework class library from ASP.NET Core 2.2 Web API project ASP.NET Core DI在类库中? - ASP.NET Core DI in a class library? 引用ASP.NET Core类库 - Reference an ASP.NET Core Class Library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM