【发布时间】:2013-10-19 09:06:20
【问题描述】:
我正在使用默认预设项目在其上构建我自己的应用程序。
我添加了我自己的控制器MyController 和一个新的视图目录My 和index.cshtml。
这是控制器代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebProject1.Controllers
{
public class MyController: Controller
{
//
// GET: /My/
public ActionResult Index()
{
return View();
}
}
}
这是我的RouteConfig.cs:
// ...
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "My", action = "Index", id = UrlParameter.Optional }
);
}
}
// ...
但是,当我开始调试并导航到 /My/ 时,会显示 404 错误,并显示一条类似于 The resource or one of its dependencies cannot be found. 的消息。
如何让我的控制器工作?
【问题讨论】:
-
请同时发布您的路线代码。路由中提到的默认控制器是什么?
-
你有索引视图吗?
-
@HBhatia 是的,我创建了
/Views/My/Index.cshtml。 -
路线看起来也正确。您可以发布浏览器正在搜索您的视图的位置吗?或者您是否通过保留断点来检查操作索引是否被命中。如果在浏览器中输入 /my/index 是否有效?
-
@ckv 嗯,不,控制器中的
Index()方法没有被命中。即使我手动输入完整的 URL。
标签: asp.net-mvc asp.net-mvc-4 http-status-code-404