4

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.

Chris Hadfield
  • 494
  • 1
  • 7
  • 34
  • Perhaps [this](https://stackoverflow.com/questions/47617994/how-to-use-a-controller-in-another-assembly-in-asp-net-core-mvc-2-0). The controllers are referenced, but the app doesn't know to look for controllers in any assembly but its own. – Scott Hannen Apr 29 '19 at 16:22
  • 1
    Why did you put the controllers in a separate project anyways? That's not really required for an N-Tier app. – mason Apr 29 '19 at 16:26
  • @mason Api controller are only on different project but mvc controller are in main project. Is it bad practice to have api controller in different class – Chris Hadfield Apr 29 '19 at 16:27
  • It's not necessarily bad. But what advantage does it give you? I can't think of any. And if it's making it harder to build/run your app....I think you should think of the positives and negatives of your approach and decide whether it's worth it. – mason Apr 29 '19 at 16:29
  • @mason Can you tell me how to call an web api without exposing it on network tab of browser. – Chris Hadfield Apr 29 '19 at 16:55
  • Why would you want to do that? – mason Apr 29 '19 at 17:16
  • @mason Coz I don't wanna show how I consume my api to others – Chris Hadfield Apr 29 '19 at 17:19
  • You don't want others to be able to hit your Web API? Who is the intended audience for it? Who should be to talk to it? – mason Apr 29 '19 at 17:20
  • @mason I just want to know it. Can this be possible – Chris Hadfield Apr 29 '19 at 17:23
  • I don't even know what you're asking so I can't tell you whether it's possible or not. Please answer my question: who should be able to talk to your Web API? What code will be responsible for calling your Web API? – mason Apr 29 '19 at 17:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/192588/discussion-between-chris-hadfield-and-mason). – Chris Hadfield Apr 29 '19 at 17:25

1 Answers1

3

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

Ryan
  • 19,118
  • 10
  • 37
  • 53