从ASP.NET Core中的类库加载和注册API控制器 - c#

我正在使用ASP.NET Core 1.0.1。我有以下

使用"Microsoft.AspNetCore.Mvc": "1.0.1"来开发控制器的类库:

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)
        {
        }
    }
}

这是我的类libray的project.json:

{
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "NETStandard.Library": "1.6.0"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  }
}

Asp.net核心应用程序(Web API模板)将托管我的控制器和参考
该类库。但是,它永远不会达到
控制器。这是我在Web应用程序中的启动类:

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Reflection;
using CoreAPIsLibrary.Controllers;

namespace APIsHost
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc()
              .AddApplicationPart(typeof(ValuesContoller).GetTypeInfo().Assembly).AddControllersAsServices();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMvc(routes =>
            {
                routes.MapRoute("default", "{controller}/{action}/{id}");
            });
            //app.UseMvc();
        }
    }
}

我还检查了是否注入了控制器:

从ASP.NET Core中的类库加载和注册API控制器 - c#

那么,缺少了什么呢?

参考方案

也许您做错了什么。因此,这是完成此工作的步骤。

创建一个新项目:ASP.NET Core Web应用程序(.NET Core);
选择Web API模板;
运行项目并访问“ api / values”以确保其正常运行;
将一个新项目添加到名为ClassLibrary的解决方案中:类库(.NET Core);
删除Class1.cs并创建一个TestController.cs类;
在ClassLibrary项目的project.json中添加MVC依赖项:

"dependencies": {
  "NETStandard.Library": "1.6.0",
  "Microsoft.AspNetCore.Mvc": "1.0.0"
},

更新您的TestController.cs像这样:

[Route("api/[controller]")]
public class TestController : Controller{
  [HttpGet]
  public IEnumerable<string> Get() {
    return new string[] { "test1", "test2" };
  }
}

在您的WebAPI项目中添加对ClassLibrary的引用:右键单击“引用”->“添加引用...”,或按以下方式更新project.json:

"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-*"
},

更新您的Startup.cs ConfigureServices方法:

public void ConfigureServices(IServiceCollection services) {
  services.AddMvc().AddApplicationPart(Assembly.Load(new AssemblyName("ClassLibrary")));
}

再次运行项目并访问“ api / test”;

从ASP.NET FileUpload控件使用Java脚本加载图像 - c#

我试图在用户使用asp.net fileupload控件<asp:Image ID="imgPicture" width="200" height="200" runat="server" />选择一个图像后,在图像中加载一个asp图像框<asp:FileUpl…

ddl在服务器中未更新-asp.net - javascript

我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……

ASP.NET Core-使用Windows身份验证进行授权 - c#

我已将我的Web API配置为与Windows身份验证一起使用。我的目标实质上是根据用户的Windows帐户来限制控制器中的某些操作。一些将能够执行读取操作,而其他一些将能够执行将写入基础数据库的操作。我找到了大量有关如何设置基于声明的授权的文档,这是我认为我需要走的路。我还没有找到如何使用Windows身份验证进行设置。我想我缺少中间步骤,例如将Windo…

Asp.net Core 3.0-无法加载静态文件 - c#

我刚刚部署了一个新的asp.net core 3.0项目,但似乎大多数文件都没有加载,例如boostrap,css文件和大量图像。 (因为网站无处不在)我浏览了该项目(根解决方案)并将其包含在我的项目中。 (在线删除了整个解决方案,然后一次又一次地重新发布),甚至重启了VS。这是我的statup.cs app.UseStaticFiles(new Stati…

ASP.NET Core-Swashbuckle不创建swagger.json文件 - c#

我在获取Swashbuckle.AspNetCore(1.0.0)包来生成任何输出时遇到麻烦。我阅读了swagger.json文件,应将其写入“〜/ swagger / docs / v1”。但是,我没有得到任何输出。我从一个全新的ASP.NET Core API项目开始。我应该提到这是ASP.NET Core2。该API可以正常工作,并且我可以从值控制器中…