如何在macOS的Visual Code中运行Windows开发的ASP.NET Core项目
在macOS上运行Windows ASP.NET C#项目的分步解决方案
问题概述
我使用Windows系统的Visual Studio开发ASP.NET C#项目已有多年,近期购入MacBook Pro后,安装了Visual Code及SDK,但无论是通过终端还是调试功能均无法运行项目。IIS无异常,但出现Microsoft.WebApplication.Targets或csc相关报错。已配置json文件,但仍存在targets错误,需要在macOS上运行该Windows项目的分步指导。
项目文件片段
<PropertyGroup> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> </PropertyGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <!--<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />--> <!--<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />--> <ProjectExtensions> <VisualStudio> <FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}"> <WebProjectProperties> <UseIIS>True</UseIIS> <AutoAssignPort>True</AutoAssignPort> <DevelopmentServerPort>1830</DevelopmentServerPort> <DevelopmentServerVPath>/</DevelopmentServerVPath> <IISUrl>http://localhost:1830/</IISUrl> <NTLMAuthentication>False</NTLMAuthentication> <UseCustomServer>False</UseCustomServer> <CustomServerUrl> </CustomServerUrl> <SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile> </WebProjectProperties> </FlavorProperties> </VisualStudio> </ProjectExtensions> <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> <PropertyGroup> <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText> </PropertyGroup> <Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props'))" /> <Error Condition="!Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" /> </Target> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. <Target Name="BeforeBuild"> </Target> <Target Name="AfterBuild"> </Target> --> </Project>
分步解决方案
1. 确认项目类型与.NET环境
- 如果你的项目是传统.NET Framework(非Core):macOS原生不支持完整的.NET Framework,需安装Mono来兼容,优先推荐迁移到.NET 6+(跨平台)。
- 如果是ASP.NET Core:确保安装了对应版本的.NET SDK,在终端执行
dotnet --version确认版本匹配。
2. 修复Microsoft.WebApplication.Targets缺失
这个文件是Windows Visual Studio专属组件,macOS没有,通过NuGet安装替代包:
- 打开终端,进入项目根目录,执行:
dotnet add package Microsoft.WebApplication.targets - 修改项目文件(.csproj),注释掉原有的VSToolsPath配置,替换为NuGet包导入:
<!-- 注释掉原有PropertyGroup --> <!-- <PropertyGroup> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> </PropertyGroup> --> <!-- 添加NuGet包的targets导入 --> <Import Project="../packages/Microsoft.WebApplication.targets.*\build\Microsoft.WebApplication.targets" Condition="Exists('../packages/Microsoft.WebApplication.targets.*\build\Microsoft.WebApplication.targets')" />
3. 修复NuGet包路径与还原
- 项目文件里的Windows风格路径
\替换为macOS的/,比如把..\packages改成../packages - 在终端执行包还原命令:
dotnet restore - 如果仍有
Microsoft.Net.Compilers相关错误,更新该包到兼容macOS的版本:dotnet add package Microsoft.Net.Compilers --version 4.8.0 dotnet add package Microsoft.CodeDom.Providers.DotNetCompilerPlatform --version 4.8.0
4. 替换IIS为Kestrel服务器
macOS没有IIS,修改项目文件里的Web配置:
<WebProjectProperties> <UseIIS>False</UseIIS> <UseCustomServer>True</UseCustomServer> <CustomServerUrl>http://localhost:5000/</CustomServerUrl> <!-- 保留其他必要配置 --> <AutoAssignPort>True</AutoAssignPort> <DevelopmentServerPort>5000</DevelopmentServerPort> <DevelopmentServerVPath>/</DevelopmentServerVPath> <NTLMAuthentication>False</NTLMAuthentication> <SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile> </WebProjectProperties>
5. 配置VS Code调试与编译
- 生成
launch.json:在VS Code中按F5,选择.NET环境,自动生成配置后,修改program路径为编译后的dll路径,示例:{ "version": "0.2.0", "configurations": [ { "name": ".NET Core Launch (web)", "type": "coreclr", "request": "launch", "preLaunchTask": "build", "program": "${workspaceFolder}/bin/Debug/net6.0/YourProjectName.dll", "args": [], "cwd": "${workspaceFolder}", "stopAtEntry": false, "serverReadyAction": { "action": "openExternally", "pattern": "\\bNow listening on:\\s+(https?://\\S+)" }, "env": { "ASPNETCORE_ENVIRONMENT": "Development" }, "sourceFileMap": { "/Views": "${workspaceFolder}/Views" } } ] } - 生成
tasks.json:确保编译任务正确,示例:{ "version": "2.0.0", "tasks": [ { "label": "build", "command": "dotnet", "type": "process", "args": [ "build", "${workspaceFolder}/YourProjectName.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" } ] }
6. 测试运行
- 在终端执行
dotnet build确认编译无错误 - 执行
dotnet run启动项目,访问配置的http://localhost:5000测试
内容的提问来源于stack exchange,提问作者YASER AZARI MOGHANJUGHI
相关产品推荐
相关产品推荐

