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