ASP.NET MVC中Ajax调用Patientinsurance接口出现404(Not Found)问题排查
在ASP.NET MVC项目中,发起GET请求访问https://localhost:44381/PatientList/PatientHealth/Patientinsurance时出现404(Not Found)错误,以下是相关代码及排查建议:
相关代码
Ajax请求代码
$.ajax({ type: "GET", url: '@Url.Action("Patientinsurance", "PatientHealth", new { Area = "PatientList" })', data: { id: patientId }, beforeSend: function () { }, success: function (data) { $("#list_insurance").html(data); }, error: function (xhr, status, error) { } });
控制器代码
[HttpGet] public ActionResult Patientinsurance(int id) { insurance patientinslist = new insurance(); patientinslist.Patientsins = (from a in db.tbl_InsuranceDetails.Where(a => a.lng_PatientID == id && a.bli_Delete != true) select new insurance { ins_patientId = a.lng_PatientID, insuanceId=a.lng_InsuranceId, starNumber = a.lng_StarHealth_Number, }).ToList(); return PartialView("~/Areas/PatientList/Views/PatientHealth/Insurance_List.cshtml", patientinslist); }
项目资源
- jQuery引用:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
项目区域结构截图:

路由配置截图:

排查步骤
验证区域路由配置
确保PatientList区域的AreaRegistration类正确配置路由,且全局路由注册时先执行区域注册:// PatientListAreaRegistration.cs public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "PatientList_default", "PatientList/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); }在
Global.asax.cs或RouteConfig.cs中,确保AreaRegistration.RegisterAllAreas();语句放在默认路由注册之前。确认控制器区域归属
检查PatientHealthController.cs是否位于Areas/PatientList/Controllers目录下,可在控制器类上显式添加[Area("PatientList")]特性,避免区域识别异常。检查路由参数匹配
- 确保请求中传递的
id是有效整数,Action方法要求int类型参数,若传递非整数或空值会导致路由不匹配。 - 可给Action添加显式路由特性强制匹配:
[HttpGet] [Route("PatientList/PatientHealth/Patientinsurance/{id:int}")] public ActionResult Patientinsurance(int id) { // ... 原有代码 }
- 确保请求中传递的
验证生成的请求URL
在浏览器开发者工具中查看实际请求的URL,确认Url.Action生成的路径是否正确(如/PatientList/PatientHealth/Patientinsurance?id=xxx),检查区域、控制器、Action名称的拼写和大小写是否一致。检查视图路径有效性
确保Insurance_List.cshtml存在于~/Areas/PatientList/Views/PatientHealth/路径下,避免Action执行后因视图不存在返回500错误(此情况不会返回404,但需排除)。
内容的提问来源于stack exchange,提问作者Nithieesh S

