我正在使用默认预设项目来构建我自己的应用程序。
我已经添加了我自己的控制器MyController
和一个新的查看目录My
与index.cshtml
。为什么我自己的控制器会导致404错误?
这是控制器的代码:
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.
线的消息被显示。
如何让我的控制器工作?
也请发表您的航线代码。你在路由中提到的默认控制器是什么? – ckv
你有索引视图吗? – HBhatia
@HBhatia是的,我已经创建了'/ Views/My/Index.cshtml'。 –