locked
Problem to access controller from external library .NET Framework WEB API RRS feed

  • Question

  • User1494715010 posted

    I try to do a demo project in .net framework web api . I create the default controller which work properly and now create an external controller from a new class library but i can't access it. Here is the code from default controller which work

    public class TestController : ApiController
    {
        // GET: api/Test
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
    }

    here the code for the external controller from the class library

        public class ExternalController : ApiController
    {
        [HttpGet]
        public string Get()
        {
            return "Hello from external controller";
        }
    }

    the wepapiconfig

     public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
    
            // Web API routes
            config.MapHttpAttributeRoutes();
    
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

    and global.asax

    public class WebApiApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                GlobalConfiguration.Configure(WebApiConfig.Register);
            }
        }

    Also i add class library's reference to the main project and put ControllerBuilder.Current.DefaultNamespaces.Add("....."); but still not work.

    Wednesday, January 23, 2019 9:47 AM

All replies

  • User1120430333 posted

    Also i add class library's reference to the main project and put ControllerBuilder.Current.DefaultNamespaces.Add("....."); but still not work.

    One would think that you would use something like a Console program and use HTTPclinet() to access a Get or Post Action method in the Controller of a WebAPI solution in a test scenario.

    Yeah,  you can have an ASP.NET MVC project set project reference to a ASP.NET WebAPI project and start testing that way too, but you will eventually have to remove the project reference for the WebAPI project and start using HTTPClinet(), becuase your goal is to have the WebAPI solution hosted on IIS.

    Wednesday, January 23, 2019 11:39 AM
  • User36583972 posted

    Hi mitsosathens,

    You may need to take care is both project should use same version of Web Api library. Most probably your class library and Web api web project are using different versions of Web Api library.

    For more detailed:

    No type was found for the external controller lib?
    https://forums.asp.net/t/2060810.aspx?No+type+was+found+for+the+external+controller+lib+


    Best Regards,

    Yong Lu

    Thursday, January 24, 2019 5:27 AM