You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

首次使用Mac版VS2017遇System.Web.HttpException资源未找到错误

解决Mac上运行ASP.NET时出现的System.Web.HttpException「资源无法找到」错误

刚从Windows Visual Studio转到Mac环境跑ASP.NET项目遇到404,这种情况我碰过不少,咱们从核心问题入手一步步解决。

问题核心分析

从错误详情能看到,请求的URL是/——也就是服务器在根目录下找不到对应的默认资源。Windows IIS默认会自动识别Default.aspxIndex.aspx这类常见起始页,但Mac上运行ASP.NET依赖的Mono环境,默认文档列表大概率没包含你的Login.aspx,这是最直接的诱因。

错误详情

System.Web.HttpException The resource cannot be found.
Description: HTTP 404.The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Details: Requested URL: /
Exception stack trace:
at System.Web.StaticFileHandler.ProcessRequest (System.Web.HttpContext context) [0x00094] in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web/StaticFileHandler.cs:77
at System.Web.DefaultHttpHandler.BeginProcessRequest (System.Web.HttpContext context, System.AsyncCallback callback, System.Object state) [0x0007f] in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web/DefaultHttpHandler.cs:101
at System.Web.HttpApplication+d__225.MoveNext () [0x008d4] in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web/HttpApplication.cs:1335
at System.Web.HttpApplication.Tick () [0x00000] in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web/HttpApplication.cs:927

分步解决方案

1. 在Web.config中配置默认文档

你的Web.config目前没有指定默认起始页,需要在<system.web>节点下添加defaultDocument配置,把Login.aspx设为根目录默认加载的页面:

修改后的Web.config:

<?xml version="1.0"?>
<!-- Web.config file for fyp_codes. The settings that can be used in this file are documented at http://www.mono-project.com/Config_system.web and http://msdn2.microsoft.com/en-us/library/b5ysx397.aspx -->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5">
<assemblies />
</compilation>
<httpRuntime targetFramework="4.5" />
<!-- 添加默认文档配置 -->
<defaultDocument>
  <files>
    <add value="Login.aspx" />
  </files>
</defaultDocument>
</system.web>
</configuration>

2. 调整项目启动URL设置

确认你在Mac上用的开发工具(比如Visual Studio for Mac、Rider)里,项目的启动URL是不是指向/Login.aspx,而不是根目录/。如果启动URL是/,手动改成/Login.aspx,这样启动时会直接加载登录页。

3. 验证文件路径与大小写(Mac必查)

Mac是区分文件名大小写的,Windows不区分,这很容易踩坑:

  • 确保Login.aspx确实在项目根目录,没放在子文件夹里
  • 确认后台代码文件Login.aspx.cs存在,且命名大小写和页面的Inherits="fyp_codes.Login"完全匹配
  • 页面里引用的图片路径(比如~/images/PlantManager.jfif)也要注意大小写,避免后续加载资源时出问题

4. 重新编译项目

虽然你说依赖项没问题,但还是建议清理项目的binobj文件夹,然后重新生成——有时候跨环境迁移会残留旧编译文件,导致异常。

测试验证

修改配置后重启项目,访问根目录/应该就能自动加载Login.aspx;或者直接访问/Login.aspx确认页面能正常显示。

内容的提问来源于stack exchange,提问作者Chan Myae Tun

火山引擎 最新活动