是否可以在.NET Framework 4.6.1控制台应用程序上使用ASP.NET Core 2.x MVC [英] Is it possible to use ASP.NET Core 2.x MVC on .NET Framework 4.6.1 console application

查看:50
本文介绍了是否可以在.NET Framework 4.6.1控制台应用程序上使用ASP.NET Core 2.x MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将ASP.NET MVC 4.x应用程序移植到ASP.NET Core 2.2.WebApi的效果很好,但是我仍然无法通过MVC视图进行移植.我创建了一个完整的最低可行"再现,以显示我的意思.Program.cs看起来像这样:

I'm attempting to port an ASP.NET MVC 4.x application over to ASP.NET Core 2.2. WebApi works great, but I'm stuck with porting over the MVC views. I've created a full "minimally viable" repro to show what I mean. The Program.cs looks like this:

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace TestApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            string port = "9005";

            var host = new WebHostBuilder()
                .UseKestrel()
                .UseStartup<Startup>()
                .UseUrls($"http://0.0.0.0:{port}")
                .Build();

            host.Run();
        }
    }

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services
                .AddMvcCore()
                .AddRazorViewEngine()
                .AddJsonFormatters();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseMvcWithDefaultRoute();
        }
    }
}

我在Controllers \ HomeController.cs中有一个控制器,如下所示:

I have one controller in Controllers\HomeController.cs that looks like this:

using Microsoft.AspNetCore.Mvc;

namespace TestApp.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}

我在Views \ Home \ Index.cshtml中有一个视图,如下所示:

I have one view at Views\Home\Index.cshtml that looks like this:

@{
    var x = "Test";
}

Home page

运行程序时,可以确认是否在 return View(); 处遇到了断点-没有异常或任何异常.但是,浏览器将返回错误500,并且不显示任何内容.

When I run the program, I can confirm I hit the breakpoint at return View(); - I get no exceptions or anything. However, the browser will return an error 500 and nothing is displayed.

我的理论是我缺少将Index.cshtml编译为ASP.NET Core MVC框架可以找到的机制的任何机制.我做了一些研究,但还不清楚所有这些工作原理.有一个叫做 Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation 的东西,显然可以对cshtml文件进行运行时编译,但是它具有.NET Core依赖性.在ASP.NET 4.x的世界中,我相信有一个ASP.NET编译器可以作为构建目标运行,以处理这种事情,但是还不清楚它是如何工作的.几乎每个ASP.NET Core的Visual Studio模板都是围绕.NET Core构建的,这似乎使这些东西神奇地起作用了,但我不确定如何做到.任何人都知道下一步将要使它起作用吗?谢谢!

My theory is I'm missing whatever the mechanism is that compiles the Index.cshtml into something the ASP.NET Core MVC framework can find. I've done a bit of research but it's very unclear how all this works. There is something called Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation which apparently does runtime compilation of cshtml files, but it has a .NET Core dependency. In the ASP.NET 4.x world, I believe there was an ASP.NET compiler that would run as a build target that handled this sort of thing, but it's a bit unclear how it works. Pretty much every Visual Studio template for ASP.NET Core is built around .NET Core, which seems to magically make this stuff work but I'm not exactly sure how. Anyone know what the next steps would be to get this to work? Thanks!

PS:之所以不能在.NET Core框架上运行,是因为该项目对遗留.NET Framework(Windows)代码的依赖程度很高,因此移植需要数年的时间.

PS: The reason I cannot run on the .NET Core framework is because this project has a massive amount of dependencies on legacy .NET Framework (Windows) code which would take years to port over.

编辑:这是我的整个CSPROJ文件(我尝试了一些SDK都没有用)

Here's my entire CSPROJ file (I've tried a few SDKs to no avail)

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net461</TargetFramework>
    <OutputPath>bin\$(Configuration)\</OutputPath>
    <DebugType>portable</DebugType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.2.0" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Net.Http" />
  </ItemGroup>
</Project>

推荐答案

当您的项目针对.NET Framework时,应安装

When your project targes .NET Framework you should install Microsoft.AspNetCore.Mvc.Razor.ViewCompilation package.

它包含启用Razor视图编译所需的构建时参考.

It contains build-time references required to enable Razor view compilation.

包括对您的 .csproj 文件的软件包引用:

Include its package reference to your .csproj file:

<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.2.0" PrivateAssets="All" />

此额外步骤仅适用于.net核心项目目标

在项目根目录执行以下命令,为

Execute the below command at the project root to prepare the app for a framework-dependent deployment:

dotnet publish -c Release

这篇关于是否可以在.NET Framework 4.6.1控制台应用程序上使用ASP.NET Core 2.x MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆