Reputation: 524
I am trying to make n-tier application where web api is kept on a different class library. I made a TestController
controller class in a different class library and my code goes like this
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. I had already added a reference to that class library on main project but it doesn't working. My api is not working. 404 Page Not Found error shows up while trying to access the api. This is my project structure.
What am I doing wrong please help me.
Upvotes: 4
Views: 5956
Reputation: 20116
Try to install Microsoft.AspNetCore.Mvc
package in the razor class library.
And in startup.cs use:
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. Below is my structure:
Upvotes: 2