迁移至Visual Studio 2017后ASP.NET MVC项目出现编译错误
Hey there, let's work through this Error CS0246 you're facing with the [Remote] attribute after moving your ASP.NET MVC project to Visual Studio 2017. This is a common migration hiccup, so here are the most effective fixes to try:
Verify the System.Web.Mvc Reference Details
Even if you see the DLL in your references, double-check its properties:- Right-click
System.Web.Mvcin your project's References → Select Properties. - Confirm the Version matches your project's MVC version (e.g., 5.x for MVC5).
- Set Copy Local to
True—this ensures the project uses the local DLL instead of relying on a potentially mismatched GAC version in VS2017.
- Right-click
Ensure the Correct Using Directive is Present
The[Remote]attribute lives in theSystem.Web.Mvcnamespace. Make sure your code file has:using System.Web.Mvc;If you suspect a namespace conflict, test using the fully qualified name temporarily:
[System.Web.Mvc.Remote("ActionName", "ControllerName")]Clean and Rebuild the Project
Visual Studio's cached build files can cause version conflicts. Do a full clean:- Right-click your project → Select Clean.
- Manually delete the
binandobjfolders from your project directory. - Rebuild the entire solution.
Check the Target .NET Framework
VS2017 might have adjusted your project's target framework during migration. Confirm it's compatible with your MVC version:- Right-click your project → Select Properties.
- Go to the Application tab.
- Ensure the Target framework is .NET Framework 4.5 or higher (required for MVC5 and later).
Reinstall the MVC NuGet Package
Migration can break NuGet package references. Use the Package Manager Console to reset the MVC package:- Open Tools → NuGet Package Manager → Package Manager Console.
- Run this command to uninstall the existing package:
Uninstall-Package Microsoft.AspNet.Mvc -Force - Reinstall the correct version (replace
5.2.9with your original MVC version if needed):Install-Package Microsoft.AspNet.Mvc -Version 5.2.9
Validate Web.Config Settings
Both the rootweb.configand theViews/web.configneed proper MVC configuration:- In the root
web.config, check the<assemblies>section has an entry for System.Web.Mvc (match the version to your package):<add assembly="System.Web.Mvc, Version=5.2.9.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> - In
Views/web.config, ensure the<pages/namespaces>section includes:<add namespace="System.Web.Mvc" />
- In the root
Try these steps in order—most folks fix this with either the NuGet reinstall or adjusting the reference properties. Let me know if you hit any roadblocks!
内容的提问来源于stack exchange,提问作者Venkat. D

