简体   繁体   English

在没有 .NET Core SDK 的情况下将 ASP.NET Core MVC 作为控制台应用程序项目运行

[英]Running ASP.NET Core MVC as a Console Application Project without .NET Core SDK

Problem Background问题背景

I'm creating a tool, tha generates MVC solutions(*.sln) and building(with msbuild) them so they could be deployed.我正在创建一个工具,tha 生成 MVC 解决方案(*.sln)并构建(使用 msbuild)它们以便可以部署它们。 Tool requires .NET Framework 4.5.2.工具需要 .NET Framework 4.5.2。

Now I want to generate ASP.NET Core MVC application.现在我想生成 ASP.NET Core MVC 应用程序。 Such applications could be run under 4.5.X, but I'm unsure if msbuild could handle project.json(so i'm using packages.config) and I cannot install .NET Core as every tutorial indicate as prerequisite for developing ASP.NET Core.这样的应用程序可以在 4.5.X 下运行,但我不确定 msbuild 是否可以处理 project.json(所以我使用的是 packages.config)并且我无法安装 .NET Core,因为每个教程都表明这是开发 ASP.NET 的先决条件核心。 Currently I'm planning to deploy generated applications on Windows.目前我计划在 Windows 上部署生成的应用程序。

The Problem:问题:

So instead of .NET Core project I've created a simple console application:因此,我创建了一个简单的控制台应用程序,而不是 .NET Core 项目: 在此处输入图片说明 There I've installed there all packages needed, like:我已经在那里安装了所有需要的软件包,例如:

  <package id="Microsoft.AspNetCore.Mvc" version="1.0.1" targetFramework="net452" />

And SelfHosted the application using Kestrel:并使用 Kestrel 自托管应用程序:

    public class Program {
    static void Main() {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

I've added Controller with a View.我添加了带有视图的控制器。 When I do a request, controller is hit, but View cannot be compiled in runtime:当我发出请求时,控制器被命中,但无法在运行时编译视图: 在此处输入图片说明

Is this behavior related to the fact I'm using Console Application and not ASP.NET Core Web Application ?这种行为是否与我使用的是Console Application而不是ASP.NET Core Web Application的事实有关? Is it possible to create a full-featured MVC application as a simple console application?是否可以将功能齐全的 MVC 应用程序创建为简单的控制台应用程序?

UPDATE:更新:

I think I've found a workaround inspired from reading github issues :我想我找到了一种从阅读 github问题中得到启发的解决方法:

 public void ConfigureServices(IServiceCollection services) {
        services.AddMvc()
                .AddRazorOptions(options => {
                                     var previous = options.CompilationCallback;
                                     options.CompilationCallback = context => {
                                                                       previous?.Invoke(context);
                                                                       var refs = AppDomain.CurrentDomain.GetAssemblies()
                                                                                           .Where(x => !x.IsDynamic)
                                                                                           .Select(x => MetadataReference.CreateFromFile(x.Location))
                                                                                           .ToList();
                                                                       context.Compilation = context.Compilation.AddReferences(refs);
                                                                   };
                                 });
    }

That seems to make Razor to render my view.这似乎使 Razor 呈现我的观点。 But I'm not sure yet if it can be accepted as a solution.但我不确定它是否可以被接受为解决方案。

Right now, it's not possible to build a .NET Core app using MSBuild, but it's possible to create a console application (not .NET Core) and add the same packages using NuGet (step-by-step below).目前,无法使用 MSBuild 构建 .NET Core 应用程序,但可以创建控制台应用程序(不是 .NET Core)并使用 NuGet 添加相同的包(以下分步说明)。

According to this road map , it will be possible in the near future.根据这个路线图,在不久的将来这将是可能的。

Info from the link above:来自上面链接的信息:

Q4 2016 / Q1 2017 2016 年第四季度 / 2017 年第一季度

This will be the first minor update, mainly focused on replacing .xproj/project.json with .csproj/MSBuild.这将是第一个小更新,主要集中在用 .csproj/MSBuild 替换 .xproj/project.json。 Project format update should be automatic.项目格式更新应该是自动的。 Just opening a 1.0 project will update it to the new project format.只需打开 1.0 项目即可将其更新为新的项目格式。 There will also be new functionality and improvements in the runtime and libraries.*运行时和库中还将有新功能和改进。*

EDIT (steps to create a console app with Kestrel):编辑(使用 Kestrel 创建控制台应用程序的步骤):

I created a Console application (not .NET Core console) and I was able to run Kestrel with a simple MVC API.我创建了一个控制台应用程序(不是 .NET Core 控制台),并且能够使用简单的 MVC API 运行 Kestrel。

Here's what I did:这是我所做的:

  • I saw the dependencies I use in an existing .NET Core app, then, I added them as a NuGet reference:我看到了我在现有 .NET Core 应用程序中使用的依赖项,然后将它们添加为 NuGet 引用:

     "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.Options.ConfigurationExtensions": "1.0.0"
  • Modified the main method:修改了main方法:

     static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run(); }
  • Created a Startup.cs (UPDATED with the workaround provided in the question):创建了一个 Startup.cs(更新了问题中提供的解决方法):

     public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddEnvironmentVariables(); Configuration = builder.Build(); } private IHostingEnvironment CurrentEnvironment { get; set; } private IConfigurationRoot Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddMvc().AddRazorOptions(options => { var previous = options.CompilationCallback; options.CompilationCallback = context => { previous?.Invoke(context); var refs = AppDomain.CurrentDomain.GetAssemblies() .Where(x => !x.IsDynamic) .Select(x => MetadataReference.CreateFromFile(x.Location)) .ToList(); context.Compilation = context.Compilation.AddReferences(refs); }; }); } public void Configure(IApplicationBuilder app) { app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } }
  • Add a class to be my MVC Controller:添加一个类作为我的 MVC 控制器:

     [Route("api/[controller]")] public class ValuesController : Controller { // GET api/values [HttpGet] public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; }
  • I had to copy manually the libuv.dll file to the bin folder because I was getting an error.我不得不手动将libuv.dll文件复制到 bin 文件夹中,因为我遇到了错误。

With these steps, I was able to run my console application.通过这些步骤,我能够运行我的控制台应用程序。 In the image below, you can see my project structure and Kestrel running:在下图中,您可以看到我的项目结构和 Kestrel 正在运行:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET Core 控制台应用程序的 ASP.NET Core 配置 - ASP.NET Core configuration for .NET Core console application 在没有实体框架和迁移的ASP.NET Core MVC应用程序中使用ASP.NET标识 - Using ASP.NET Identity in an ASP.NET Core MVC application without Entity Framework and Migrations 运行 .NET 核心控制台应用程序 - Running a .NET Core Console Application ASP.NET Core Web Application(.NET Framework)项目模板缺少System.Web.MVC dll - ASP.NET Core Web Application (.NET Framework) project template is missing System.Web.MVC dll asp.net 内核中的控制台应用程序和 Web 应用程序之间的区别 - Difference between a console application and Web application in asp.net core 在 Linux 上的 ASP.NET Core MVC 项目中构建身份脚手架后运行项目时出现问题 - Problem running project after Identity scaffolding in ASP.NET Core MVC project on Linux asp.net核心mvc控制台不再出现了 - asp.net core mvc console not coming up anymore 从控制台应用程序启动ASP.NET Core 1应用程序 - Launching ASP.NET Core 1 app from console application 使用 ASP.Net Core 控制台应用程序将文件上传到 OneDrive - Upload file to OneDrive using ASP.Net Core Console Application 参考 asp.net core web application from Z2D50972FECD376129545507F1062089Z core console application - reference asp.net core web application from .net core console application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM