将ASP.NET 4.7/Razor 3.0项目迁移至ASP.NET Core 2.0(Linux环境)
Hey there, I’ve helped folks tackle this exact scenario before—migrating old ASP.NET apps to Core without a Windows environment. Let’s walk through practical, Linux-native steps to get this done:
First, Get Your Tooling Set Up
- Install the .NET Core 2.0 SDK on your Linux distro. For Debian/Ubuntu-based systems, run:
Adjust the commands if you’re using RHEL/CentOS or another distro.wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb sudo apt-get update sudo apt-get install dotnet-sdk-2.0 - In VS Code, install the C# Dev Kit and ASP.NET Core Tools extensions—these will give you intellisense, debugging, and project scaffolding support.
Step-by-Step Migration (Manual is More Reliable Than Auto-Convert)
Auto-conversion tools (like the ones in Visual Studio Windows) don’t play well on Linux, so we’ll build a new Core project and migrate code piece by piece:
Scaffold a Fresh ASP.NET Core 2.0 Razor Pages Project
Open your terminal, navigate to your work directory, and run:dotnet new razor -n MyMigratedSite cd MyMigratedSiteThis creates the standard Core Razor Pages structure (Pages folder, appsettings.json, Startup.cs) which you’ll build on.
Migrate Razor Views & Layouts
- Copy your old
.cshtmlviews from the original project’sViewsfolder to the new project’sPagesfolder. - Move your
_Layout.cshtmland shared partials toPages/Shared(Core uses this instead ofViews/Shared). - Critical: Linux is case-sensitive! Make sure your file names match references in code (e.g., if your view references
_Layout.cshtml, don’t rename it to_layout.cshtml).
- Copy your old
Port Business Logic & Models
- Copy your old
.csfiles (Models, custom helpers, business logic) to the new project’sModelsorServicesfolder. - Update namespaces to match the new project (e.g., change
OldSite.ModelstoMyMigratedSite.Models). - Replace any references to
System.Webwith Core equivalents:System.Web.HttpContext.Current→ Use dependency injection withIHttpContextAccessor(inject it in Razor pages with@inject IHttpContextAccessor HttpContextAccessor).System.Web.Configuration.WebConfigurationManager→ UseIConfigurationfrom Core’s dependency injection.
- Copy your old
Migrate Configuration
- Replace your old
web.configsettings withappsettings.json. For example, a connection string would go from:
To this in<connectionStrings> <add name="MyDb" connectionString="..." /> </connectionStrings>appsettings.json:"ConnectionStrings": { "MyDb": "..." } - Add any custom app settings to
appsettings.jsonas key-value pairs.
- Replace your old
Fix NuGet Dependencies
- Remove any
System.Web-related packages from your old project—they won’t work in Core. - Add Core-compatible packages via the terminal:
dotnet add package Microsoft.AspNetCore.Session # If you use session state dotnet add package Microsoft.EntityFrameworkCore.SqlServer # For DB access
- Remove any
Configure Startup.cs
- Add essential services to
ConfigureServices:public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddSession(); // Enable session if needed services.AddHttpContextAccessor(); // For accessing HttpContext // Add DB context, authentication, etc., here } - Update the
Configuremethod to use middleware:public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseSession(); // Add this before UseRouting if using session app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); }); }
- Add essential services to
Linux-Specific Tips
- File Permissions: Ensure your project directory has read/write permissions for the user running the app:
chmod -R 755 MyMigratedSite - Debugging in VS Code: Use the built-in debugger (F5) to catch runtime errors. The C# extension will auto-generate a
launch.jsonfile for you. - Testing Locally: Run
dotnet runin your project directory to start the app, then visithttps://localhost:5001to test pages.
Troubleshooting Common Issues
- Missing Namespaces: Use VS Code’s "Quick Fix" (Ctrl+. on Linux) to auto-add required using statements for Core APIs.
- View Compilation Errors: Ensure all Razor pages have the correct
@pagedirective at the top (required for Core Razor Pages). - Session Not Working: Double-check that
app.UseSession()comes beforeapp.UseRouting()inStartup.cs.
内容的提问来源于stack exchange,提问作者TemporalBeing

