Running ASP.NET Core MVC as a console application without the .NET Core SDK

Main problem

I am creating a tool that generates MVC solutions (*. Sln) and builds (using msbuild) so that they can be deployed. The tool requires the .NET Framework 4.5.2.

Now I want to create an ASP.NET Core MVC application. Such applications can be run under 4.5.X, but I am not sure that msbuild can handle project.json (so I use package.config), and I can not install .NET Core, since each tutorial indicates that this is a prerequisite for ASP.NET core development. I am currently planning to deploy the generated applications on Windows.

Problem:

So, instead of the .NET Core project, I created a simple console application: enter image description here There I installed all the necessary packages, for example:

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

SelfHosted Kestrel:

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

        host.Run();
    }
}

. , , View : enter image description here

, , ASP.NET Core Web Application? MVC ?

UPDATE:

, , github issues:

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

, Razor . , .

+4
1

.NET Core MSBuild. ( .NET Core) Nuget ( ).

, .

:

Q4 2016/Q1 2017

, .xproj/project.json .csproj/MSBuild. . 1.0 . . *

EDIT ( ):

( .NET Core), API MVC.

:

  • , .net, 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"
    
  • :

    static void Main(string[] args) {
      var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();
      host.Run();
    }
    
  • 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?}");
          });
        }
      }
    
  • MVC-:

      [Route("api/[controller]")]
      public class ValuesController : Controller {
    
        // GET api/values
        [HttpGet]
        public IEnumerable<string> Get() {
          return new string[] { "value1", "value2" };
        }
    
  • mannualy libuv.dll bin, .

. :

enter image description here

+5

Source: https://habr.com/ru/post/1658192/