How can I route an External Class Library in my Web API, ASP NET 5

时光毁灭记忆、已成空白 提交于 2019-12-20 07:48:59

问题


I have created an WEB API in ASP NET 5 and I can reference an external Class Library vNext. I am working on Visual Studio 2015 Community. In that Library I have this controller:

[Route("api/[controller]")]
public class NovosController : Controller
{
    // GET: api/values

    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "i am", "an external library" };
    }

To add this library in my Web API´s references I had to browse the DLL's (one for dnxcore50 and one for dnx451).

This is the result:

dnx451

dnxcore50

In my web API I can get the data from that controller but I can't acess it from URL. But if the library is in the same Solution I can acess it from the URL.

For example:

If external library:

localhost:51000/api/novos

returns me nothing

but if the library is in the same solution:

localhost:51000/api/novos

returns me " i am an external library"

I want to acess by URL to the external library but I can't find any solution to my problem, there is anyone that knows how to make this thing work?


回答1:


You don't need anything special to allow your external class library to be discovered by IControllerTypeProvider as long as you comply with the requisites:

  • has to be class
  • can’t be abstract
  • has to be public
  • has to be top-level (not nested)
  • can’t be generic
  • has to either derive from Controller base class or end in Controller suffix (for POCOs) and be located in an assembly that references MVC assembly

(Source)

In your particular case, I think you just need to remove the Route annotation, since it doesn't looks right.

using Microsoft.AspNet.Mvc;
using System.Collections.Generic;

namespace External.Controllers
{
    [Route("api/[controller]")]
    public class NovosController: Controller
    {
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "I am", "an external library" };
        }
    }
}


来源:https://stackoverflow.com/questions/35623383/how-can-i-route-an-external-class-library-in-my-web-api-asp-net-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!