如何将新建的.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.
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
wwwrootfolder from your .NET Core project into the root of the new Framework project. - Right-click the
wwwrootfolder → Properties: Set "Copy to Output Directory" to "Copy if newer" to ensure static files deploy correctly. - Update your
Web.configto serve files fromwwwroot:
Add this inside the<system.webServer>node:
If you want cleaner URLs (like<handlers> <add name="StaticFile" path="wwwroot/*" verb="*" modules="StaticFileModule" resourceType="Either" requireAccess="Read" /> </handlers>/static/instead of/wwwroot/), you can add URL rewrite rules, but keeping thewwwrootpath 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
.csprojfile 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.
- Open the
4. Migrate Code & Adapt API Differences
- Controllers & Views: Most code can be copied directly, but adjust these key differences:
- Replace Core's
IHostingEnvironmentwithHttpContext.Current.Server.MapPathto access file paths, or installMicrosoft.Extensions.Configurationto 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.
- Replace Core's
- Configuration: You can keep
appsettings.jsonby usingMicrosoft.Extensions.Configuration.Jsonto read it, or move settings to the<appSettings>section inWeb.configfor 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.csto the Framework project'sApp_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.asaxcallsRouteConfig.RegisterRoutes(RouteTable.Routes);in itsApplication_Startmethod.
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

