首页 新闻 会员 周边 捐助

MVC5开发遇到404错误

0
悬赏园豆:50 [已解决问题] 解决于 2017-05-26 14:55

上面是我的错误截图。然后是我的系统资源管理器截图:

(1)Controllers

ProjectItemsController.cs代码:

  1 using Apps.Common;
  2 using Apps.IBLL.Sys;
  3 using Apps.Locale;
  4 using Apps.Models.Sys;
  5 using Apps.Web.Core;
  6 using Microsoft.Practices.Unity;
  7 using System.Collections.Generic;
  8 using System.Web.Mvc;
  9 
 10 namespace Apps.Web.Controllers
 11 {
 12     public class ProjectItemsController : BaseController
 13     {
 14         [Dependency]
 15         public IProjectItemsBLL m_BLL { get; set; }
 16         ValidationErrors errors = new ValidationErrors();
 17         
 18         [SupportFilter]
 19         public ActionResult Index()
 20         {
 21             return View();
 22         }
 23         [HttpPost]
 24         [SupportFilter(ActionName="Index")]
 25         public JsonResult GetList(GridPager pager, string queryStr)
 26         {
 27             List<ProjectItemsModel> list = m_BLL.GetList(ref pager, queryStr);
 28             GridRows<ProjectItemsModel> grs = new GridRows<ProjectItemsModel>();
 29             grs.rows = list;
 30             grs.total = pager.totalRows;
 31             return Json(grs);
 32         }
 33         #region 创建
 34         [SupportFilter]
 35         public ActionResult Create()
 36         {
 37             return View();
 38         }
 39 
 40         [HttpPost]
 41         [SupportFilter]
 42         public JsonResult Create(ProjectItemsModel model)
 43         {
 44             model.ID = ResultHelper.NewId;
 45             model.CreateTime = ResultHelper.NowTime;
 46             if (model != null && ModelState.IsValid)
 47             {
 48 
 49                 if (m_BLL.Create(ref errors, model))
 50                 {
 51                     LogHandler.WriteServiceLog(GetUserId(), "ID" + model.ID + ",Name" + model.Name, "成功", "创建", "ProjectItems");
 52                     return Json(JsonHandler.CreateMessage(1, Resource.InsertSucceed));
 53                 }
 54                 else
 55                 {
 56                     string ErrorCol = errors.Error;
 57                     LogHandler.WriteServiceLog(GetUserId(), "ID" + model.ID + ",Name" + model.Name + "," + ErrorCol, "失败", "创建", "ProjectItems");
 58                     return Json(JsonHandler.CreateMessage(0, Resource.InsertFail + ErrorCol));
 59                 }
 60             }
 61             else
 62             {
 63                 return Json(JsonHandler.CreateMessage(0, Resource.InsertFail));
 64             }
 65         }
 66         #endregion
 67 
 68         #region 修改
 69         [SupportFilter]
 70         public ActionResult Edit(string id)
 71         {
 72             ProjectItemsModel entity = m_BLL.GetById(id);
 73             return View(entity);
 74         }
 75 
 76         [HttpPost]
 77         [SupportFilter]
 78         public JsonResult Edit(ProjectItemsModel model)
 79         {
 80             if (model != null && ModelState.IsValid)
 81             {
 82 
 83                 if (m_BLL.Edit(ref errors, model))
 84                 {
 85                     LogHandler.WriteServiceLog(GetUserId(), "ID" + model.ID + ",Name" + model.Name, "成功", "修改", "ProjectItems");
 86                     return Json(JsonHandler.CreateMessage(1, Resource.EditSucceed));
 87                 }
 88                 else
 89                 {
 90                     string ErrorCol = errors.Error;
 91                     LogHandler.WriteServiceLog(GetUserId(), "ID" + model.ID + ",Name" + model.Name + "," + ErrorCol, "失败", "修改", "ProjectItems");
 92                     return Json(JsonHandler.CreateMessage(0, Resource.EditFail + ErrorCol));
 93                 }
 94             }
 95             else
 96             {
 97                 return Json(JsonHandler.CreateMessage(0, Resource.EditFail));
 98             }
 99         }
100         #endregion
101 
102         #region 详细
103         [SupportFilter]
104         public ActionResult Details(string id)
105         {
106             ProjectItemsModel entity = m_BLL.GetById(id);
107             return View(entity);
108         }
109 
110         #endregion
111 
112         #region 删除
113         [HttpPost]
114         [SupportFilter]
115         public JsonResult Delete(string id)
116         {
117             if (!string.IsNullOrWhiteSpace(id))
118             {
119                 if (m_BLL.Delete(ref errors, id))
120                 {
121                     LogHandler.WriteServiceLog(GetUserId(), "Id:" + id, "成功", "删除", "ProjectItems");
122                     return Json(JsonHandler.CreateMessage(1, Resource.DeleteSucceed));
123                 }
124                 else
125                 {
126                     string ErrorCol = errors.Error;
127                     LogHandler.WriteServiceLog(GetUserId(), "ID" + id + "," + ErrorCol, "失败", "删除", "ProjectItems");
128                     return Json(JsonHandler.CreateMessage(0, Resource.DeleteFail + ErrorCol));
129                 }
130             }
131             else
132             {
133                 return Json(JsonHandler.CreateMessage(0, Resource.DeleteFail));
134             }
135 
136 
137         }
138         #endregion
139     }
140 }
View Code

 

(2)view

Index.cshtml代码:

  1 @using Apps.Web.Core;
  2 @using Apps.Models.Sys;
  3 @using Apps.Locale;
  4 @{
  5     ViewBag.Title = "ProjectItems";
  6     Layout = "~/Views/Shared/_Index_Layout.cshtml";
  7     List<permModel> perm = null;
  8 }
  9 <div class="mvctool">
 10     <input id="txtQuery" type="text" class="searchText" />
 11     @Html.ToolButton("btnQuery", "fa fa-search", Resource.Query,ref perm, "Query", true)
 12     @Html.ToolButton("btnCreate", "fa fa-plus", Resource.Create,ref perm, "Create", true)
 13     @Html.ToolButton("btnEdit", "fa fa-pencil", Resource.Edit,ref perm, "Edit", true)
 14     @Html.ToolButton("btnDetails", "fa fa-list", Resource.Details,ref perm, "Details", true)
 15     @Html.ToolButton("btnDelete", "fa fa-trash", Resource.Delete,ref perm, "Delete", true)
 16 </div>
 17 <table id="List"></table>
 18 
 19 <div id="modalwindow" class="easyui-window" style="width:800px; height:400px;" data-options="modal:true,closed:true,minimizable:false,shadow:false"></div>
 20 @Html.Partial("~/Views/Shared/_Partial_AutoGrid.cshtml")
 21 <script type="text/javascript">
 22     $(function () {
 23         $('#List').datagrid({
 24             url: '@Url.Action("GetList")',
 25             width:SetGridWidthSub(10),
 26             methord: 'post',
 27             height: SetGridHeightSub(45),
 28             fitColumns: true,
 29             sortName: 'CreateTime',
 30             sortOrder: 'desc',
 31             idField: 'ID',
 32             pageSize: 15,
 33             pageList: [15, 20, 30, 40, 50],
 34             pagination: true,
 35             striped: true, //奇偶行是否区分
 36             singleSelect: true,//单选模式
 37             //rownumbers: true,//行号
 38             columns: [[
 39                 { field: 'ID', title: 'ID', width: 80,sortable:true },
 40                 { field: 'Name', title: 'Name', width: 80,sortable:true },
 41                 { field: 'Status', title: 'Status', width: 80,sortable:true },
 42                 { field: 'InvestTotal', title: 'InvestTotal', width: 80,sortable:true },
 43                 { field: 'ConScaleContent', title: 'ConScaleContent', width: 80,sortable:true },
 44                 { field: 'InvestmentSubject', title: 'InvestmentSubject', width: 80,sortable:true },
 45                 { field: 'Trade', title: 'Trade', width: 80,sortable:true },
 46                 { field: 'WarehouseYear', title: 'WarehouseYear', width: 80,sortable:true },
 47                 { field: 'DeclarationCompany', title: 'DeclarationCompany', width: 80,sortable:true },
 48                 { field: 'DeclarationDate', title: 'DeclarationDate', width: 80,sortable:true },
 49                 { field: 'originatorId', title: 'originatorId', width: 80,sortable:true },
 50                 { field: 'CreateTime', title: 'CreateTime', width: 80,sortable:true }
 51             ]]
 52         });
 53     });
 54     //ifram 返回
 55     function frameReturnByClose() {
 56         $("#modalwindow").window('close');
 57     }
 58     function frameReturnByReload(flag) {
 59         if (flag)
 60             $("#List").datagrid('load');
 61         else
 62             $("#List").datagrid('reload');
 63     }
 64     function frameReturnByMes(mes) {
 65         $.messageBox5s('@Resource.Tip', mes);
 66     }
 67     $(function () {
 68         $("#btnCreate").click(function () {
 69             $("#modalwindow").html("<iframe width='100%' height='100%' scrolling='auto' frameborder='0'' src='@Url.Action("Create")'></iframe>");
 70             $("#modalwindow").window({ title: '@Resource.Create', width: 700, height: 400, iconCls: 'fa fa-plus' }).window('open');
 71         });
 72         $("#btnEdit").click(function () {
 73             var row = $('#List').datagrid('getSelected');
 74             if (row != null) {
 75                 $("#modalwindow").html("<iframe width='100%' height='99%'  frameborder='0' src='@Url.Action("Edit")?id=" + row.Id + "&Ieguid=" + GetGuid() + "'></iframe>");
 76                 $("#modalwindow").window({ title: '@Resource.Edit', width: 700, height: 400, iconCls: 'fa fa-pencil' }).window('open');
 77             } else { $.messageBox5s('@Resource.Tip', '@Resource.PlaseChooseToOperatingRecords'); }
 78         });
 79         $("#btnDetails").click(function () {
 80             var row = $('#List').datagrid('getSelected');
 81             if (row != null) {
 82                 $("#modalwindow").html("<iframe width='100%' height='98%' scrolling='auto' frameborder='0' src='@Url.Action("Details")?id=" + row.Id + "&Ieguid=" + GetGuid() + "'></iframe>");
 83                 $("#modalwindow").window({ title: '@Resource.Details', width: 700, height: 400, iconCls: 'fa fa-list' }).window('open');
 84             } else { $.messageBox5s('@Resource.Tip', '@Resource.PlaseChooseToOperatingRecords'); }
 85             });
 86         $("#btnQuery").click(function () {
 87             var queryStr = $("#txtQuery").val();
 88             if (queryStr == null) {
 89                 queryStr = "%";
 90             }
 91             $("#List").datagrid("load", { queryStr: encodeURI(queryStr) });
 92 
 93         });
 94         $("#btnDelete").click(function () {
 95             var row = $('#List').datagrid('getSelected');
 96             if (row != null) {
 97                 $.messager.confirm('@Resource.Tip', '@Resource.YouWantToDeleteTheSelectedRecords', function (r) {
 98                         if (r) {
 99                             $.post("@Url.Action("Delete")?id=" + row.Id, function (data) {
100                                 if (data.type == 1)
101                                     $("#List").datagrid('load');
102                                 $.messageBox5s('@Resource.Tip', data.message);
103                             }, "json");
104 
105                         }
106                     });
107                 } else { $.messageBox5s('@Resource.Tip', '@Resource.PlaseChooseToOperatingRecords'); }
108             });
109     });
110 </script>
View Code

 

最后是ProjectAreaRegistration.cs里面的代码:

 1 using System.Web.Mvc;
 2 
 3 namespace Apps.Web.Areas.Project
 4 {
 5     public class ProjectAreaRegistration : AreaRegistration
 6     {
 7         public override string AreaName
 8         {
 9             get
10             {
11                 return "Project";
12             }
13         }
14         public override void RegisterArea(AreaRegistrationContext context)
15         {
16             context.MapRoute(
17                 "ProjectGlobalization", // 路由名称
18                 "{lang}/Project/{controller}/{action}/{id}", // 带有参数的 URL
19                 new { lang = "zh", controller = "Home", action = "Index", id = UrlParameter.Optional }, // 参数默认值
20                 new { lang = "^[a-zA-Z]{2}-[a-zA-Z]{2}?$" }    //参数约束
21             );
22             context.MapRoute(
23                 "Project_default",
24                 "Project/{controller}/{action}/{id}",
25                 new { action = "Index", id = UrlParameter.Optional }
26             );
27         }
28     }
29 }

本人用的是Area,因为是照着人家框架新增的一个Area,基本和其他能够显示页面的内容一致,想了很多可能导致404错误的原因,但发现无论怎么改还是报这个错,不知是否有大神可以指点下迷津,谢谢。

问题补充:

我的开发环境是vs2017+EF6.0+MVC5

星尘之泪的主页 星尘之泪 | 初学一级 | 园豆:49
提问于:2017-05-26 11:45
< >
分享
最佳答案
0

你的问题应该是路由的问题,你的主路由配置和你在Area中的路由有冲突。

还有你有没有在Application_Start中注册Area中路由

如果没有在  Application_Start AreaRegistration.RegisterAllAreas();加上这段话。

你可以试着先把zh-cn去掉直接方法project/projectItem/index试试、。

收获园豆:50
Emrys5 | 菜鸟二级 |园豆:223 | 2017-05-26 12:30

主路由?是指APP_Start中的Route.config?

星尘之泪 | 园豆:49 (初学一级) | 2017-05-26 12:58

就目前状况我把zh-cn去掉还是不行

星尘之泪 | 园豆:49 (初学一级) | 2017-05-26 12:59

@星尘之泪: 对的。你先看看你有没有注册Area的路由

Emrys5 | 园豆:223 (菜鸟二级) | 2017-05-26 12:59

@Emrys5: 在哪里看有没有注册Area的路由?

这是Route.config的代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using System.Web.Routing;
 7 
 8 namespace Apps.Web
 9 {
10     public class RouteConfig
11     {
12         public static void RegisterRoutes(RouteCollection routes)
13         {
14             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
15 
16             routes.MapRoute(
17                 "Globalization", // 路由名称
18                 "{lang}/{controller}/{action}/{id}", // 带有参数的 URL
19                 new { lang = "zh", controller = "Home", action = "Index", id = UrlParameter.Optional }, // 参数默认值
20                 new { lang = "^[a-zA-Z]{2}-[a-zA-Z]{2}?$" }    //参数约束
21             );
22 
23             routes.MapRoute(
24                 "Default", // 路由名称
25                 "{controller}/{action}/{id}", // 带有参数的 URL
26                 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
27             );
28         }
29     }
30 }
星尘之泪 | 园豆:49 (初学一级) | 2017-05-26 13:02

@星尘之泪: 在Global.asax文件中 Application_Start方法中,看有没有 AreaRegistration.RegisterAllAreas();

Emrys5 | 园豆:223 (菜鸟二级) | 2017-05-26 14:22

@Emrys5: 有

星尘之泪 | 园豆:49 (初学一级) | 2017-05-26 14:39

@星尘之泪: 你访问的路径直接就匹配Globalization这个路由了

他应该是去找project Controller和ProjectItem action,肯定没有,所以包了404错误。

你在你访问的路径后面加上/index应该就可以了。

Emrys5 | 园豆:223 (菜鸟二级) | 2017-05-26 14:43

@Emrys5: 虽然还是不行,但我从头到尾重新弄了遍,把结构变成Controller/Project/Items/...cs就好了,虽然很奇怪不知道为什么,不过感谢你的回答,分数给你吧.

星尘之泪 | 园豆:49 (初学一级) | 2017-05-26 14:54
其他回答(1)
0

https://forums.iis.net/t/1160687.aspx?Server+Error+in+Application+The+resource+cannot+be+found+

风行天下12 | 园豆:3867 (老鸟四级) | 2017-05-26 11:56
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册