You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将新建的.NET Core 2.0项目转换为.NET Framework 4.7?

Hey there! Let's break down how to convert your .NET Core 2.0 project over to .NET Framework 4.7 while keeping that familiar wwwroot structure you love from your old .NET 2.0 MVC days. I'll also touch on why Microsoft's template updates focus on .NET Core first.

最佳转换步骤:.NET Core 2.0 → .NET Framework 4.7 MVC

1. Start with a .NET Framework 4.7 MVC Base Project

  • Open Visual Studio and create a new ASP.NET Web Application (.NET Framework). Select .NET Framework 4.7 as the target, then pick the MVC template. This gives you the traditional Framework MVC structure we'll tweak to match your preferences.

2. Migrate the wwwroot Folder & Static File Configuration

  • Copy the wwwroot folder from your .NET Core project into the root of the new Framework project.
  • Right-click the wwwroot folder → Properties: Set "Copy to Output Directory" to "Copy if newer" to ensure static files deploy correctly.
  • Update your Web.config to serve files from wwwroot:
    Add this inside the <system.webServer> node:
    <handlers>
      <add name="StaticFile" path="wwwroot/*" verb="*" modules="StaticFileModule" resourceType="Either" requireAccess="Read" />
    </handlers>
    
    If you want cleaner URLs (like /static/ instead of /wwwroot/), you can add URL rewrite rules, but keeping the wwwroot path works perfectly too.

3. Convert the Project File (SDK-Style → Traditional Framework Format)

  • If you're modifying your existing Core project instead of starting fresh:
    • Open the .csproj file and replace its content with this traditional Framework structure (adjust GUIDs, namespaces, and references to match your project):
      <?xml version="1.0" encoding="utf-8"?>
      <Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
        <PropertyGroup>
          <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
          <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
          <ProjectGuid>{Your-Project-GUID}</ProjectGuid>
          <ProjectTypeGuids>{E3E379DF-F4C6-4180-9B81-6769533ABE47};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
          <OutputType>Library</OutputType>
          <RootNamespace>Your.Project.Namespace</RootNamespace>
          <AssemblyName>YourProjectName</AssemblyName>
          <TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
          <MvcBuildViews>false</MvcBuildViews>
        </PropertyGroup>
        <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
          <DebugSymbols>true</DebugSymbols>
          <DebugType>full</DebugType>
          <Optimize>false</Optimize>
          <OutputPath>bin\</OutputPath>
          <DefineConstants>DEBUG;TRACE</DefineConstants>
        </PropertyGroup>
        <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
          <DebugType>pdbonly</DebugType>
          <Optimize>true</Optimize>
          <OutputPath>bin\</OutputPath>
          <DefineConstants>TRACE</DefineConstants>
        </PropertyGroup>
        <ItemGroup>
          <Reference Include="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
            <HintPath>..\packages\Microsoft.AspNet.Mvc.5.2.7\lib\net45\System.Web.Mvc.dll</HintPath>
          </Reference>
          <Reference Include="System.Web" />
          <Reference Include="System.Web.Routing" />
          <!-- Add other required Framework references here -->
        </ItemGroup>
        <ItemGroup>
          <Content Include="wwwroot\**\*" />
          <Content Include="Views\**\*.cshtml" />
        </ItemGroup>
        <ItemGroup>
          <Compile Include="Controllers\**\*.cs" />
          <Compile Include="Models\**\*.cs" />
        </ItemGroup>
        <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
      </Project>
      
    • Install necessary NuGet packages: Microsoft.AspNet.Mvc (v5.2.7+ works with .NET 4.7), Microsoft.AspNet.WebApi (if you used WebAPI), and any other dependencies from your Core project that have Framework-compatible versions.

4. Migrate Code & Adapt API Differences

  • Controllers & Views: Most code can be copied directly, but adjust these key differences:
    • Replace Core's IHostingEnvironment with HttpContext.Current.Server.MapPath to access file paths, or install Microsoft.Extensions.Configuration to keep using JSON config files if you prefer.
    • Swap Core-specific Tag Helpers with Framework MVC's Html Helpers, or install compatible Tag Helper packages for Framework.
  • Configuration: You can keep appsettings.json by using Microsoft.Extensions.Configuration.Json to read it, or move settings to the <appSettings> section in Web.config for a more traditional approach.
  • Dependency Injection: Framework MVC doesn't have Core's built-in DI. If you used DI, switch to a third-party container like Autofac or Unity, or manually instantiate services.

5. Update Routing & Startup Logic

  • Migrate routing rules from Core's Startup.cs to the Framework project's App_Start/RouteConfig.cs:
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
    
  • Make sure Global.asax calls RouteConfig.RegisterRoutes(RouteTable.Routes); in its Application_Start method.

Quick Note on Microsoft's Template Focus

You mentioned wondering why Microsoft only updates .NET Core templates—this is because .NET Core is their modern, cross-platform framework with active development, while .NET Framework is in maintenance mode (no major structural updates planned). The good news is .NET Framework 4.7 is flexible enough to replicate the wwwroot structure and Core-like workflow manually, as we've covered here.


内容的提问来源于stack exchange,提问作者Abhijeet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:07:16