Chris Hadfield
Chris Hadfield

Reputation: 524

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. 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.

enter image description here

What am I doing wrong please help me.

Upvotes: 4

Views: 5956

Answers (1)

Ryan
Ryan

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:

enter image description here

Upvotes: 2

Related Questions