Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

c# - Loading and registering API Controllers From Class Library in ASP.NET core

I am using ASP.NET Core 1.0.1. I have the following

  • A class library that uses "Microsoft.AspNetCore.Mvc": "1.0.1" in order to develop my controllers:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace CoreAPIsLibrary.Controllers
{

    [Route("api/[controller]")]
    public class ValuesContoller : Controller
    { 
        public string Get()
        {
            return "value";
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/values
        [HttpPost]
        public void Post([FromBody]string value)
        {
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody]string value)
        {
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe you're doing something wrong. So, here are the steps to make this work.

  • Create a new project: ASP.NET Core Web Application (.NET Core);
  • Choose the Web API template;
  • Run the project and access the "api/values" to make sure it's working;
  • Add a new project to the solution named ClassLibrary: Class Library (.NET Core);
  • Delete the Class1.cs and create a TestController.cs class;
  • Add the MVC dependency in the project.json from the ClassLibrary project:

    "dependencies": {
      "NETStandard.Library": "1.6.0",
      "Microsoft.AspNetCore.Mvc": "1.0.0"
    },
    
  • Update your TestController.cs to be like this:

    [Route("api/[controller]")]
    public class TestController : Controller{
      [HttpGet]
      public IEnumerable<string> Get() {
        return new string[] { "test1", "test2" };
      }
    }
    
  • Add the reference to ClassLibrary in your WebAPI Project: right-click on "References"->"Add Reference..." or update your project.json like this:

    "dependencies": {
      "Microsoft.NETCore.App": {
        "version": "1.0.0",
        "type": "platform"
      },
      "Microsoft.AspNetCore.Mvc": "1.0.0",
      "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
      "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
      "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
      "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
      "Microsoft.Extensions.Configuration.Json": "1.0.0",
      "Microsoft.Extensions.Logging": "1.0.0",
      "Microsoft.Extensions.Logging.Console": "1.0.0",
      "Microsoft.Extensions.Logging.Debug": "1.0.0",
      "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
      "ClassLibrary": "1.0.0-*"
    },
    
  • Update your Startup.cs ConfigureServices method:

    public void ConfigureServices(IServiceCollection services) {
      services.AddMvc().AddApplicationPart(Assembly.Load(new AssemblyName("ClassLibrary")));
    }
    
  • Run the project again and access "api/test";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...