-->

How can I route an External Class Library in my We

2019-03-07 02:08发布

问题:

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" };
        }
    }
}