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

将ASP.NET 4.7/Razor 3.0项目迁移至ASP.NET Core 2.0(Linux环境)

Migrating ASP.NET 4.7/Razor 3.0 to ASP.NET Core 2.0 on Linux (VS Code Only)

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:
    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
    
    Adjust the commands if you’re using RHEL/CentOS or another distro.
  • 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:

  1. 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 MyMigratedSite
    

    This creates the standard Core Razor Pages structure (Pages folder, appsettings.json, Startup.cs) which you’ll build on.

  2. Migrate Razor Views & Layouts

    • Copy your old .cshtml views from the original project’s Views folder to the new project’s Pages folder.
    • Move your _Layout.cshtml and shared partials to Pages/Shared (Core uses this instead of Views/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).
  3. Port Business Logic & Models

    • Copy your old .cs files (Models, custom helpers, business logic) to the new project’s Models or Services folder.
    • Update namespaces to match the new project (e.g., change OldSite.Models to MyMigratedSite.Models).
    • Replace any references to System.Web with Core equivalents:
      • System.Web.HttpContext.Current → Use dependency injection with IHttpContextAccessor (inject it in Razor pages with @inject IHttpContextAccessor HttpContextAccessor).
      • System.Web.Configuration.WebConfigurationManager → Use IConfiguration from Core’s dependency injection.
  4. Migrate Configuration

    • Replace your old web.config settings with appsettings.json. For example, a connection string would go from:
      <connectionStrings>
        <add name="MyDb" connectionString="..." />
      </connectionStrings>
      
      To this in appsettings.json:
      "ConnectionStrings": {
        "MyDb": "..."
      }
      
    • Add any custom app settings to appsettings.json as key-value pairs.
  5. 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
      
  6. 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 Configure method 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();
          });
      }
      

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.json file for you.
  • Testing Locally: Run dotnet run in your project directory to start the app, then visit https://localhost:5001 to 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 @page directive at the top (required for Core Razor Pages).
  • Session Not Working: Double-check that app.UseSession() comes before app.UseRouting() in Startup.cs.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:48:43