2013-10-11 84 views
-1

我正在使用默认预设项目来构建我自己的应用程序。
我已经添加了我自己的控制器MyController和一个新的查看目录Myindex.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.线的消息被显示。

如何让我的控制器工作?

+0

也请发表您的航线代码。你在路由中提到的默认控制器是什么? – ckv

+0

你有索引视图吗? – HBhatia

+0

@HBhatia是的,我已经创建了'/ Views/My/Index.cshtml'。 –

回答

0

这几乎肯定是某处的错字。我建议重新创建你的控制器和视图。

0

除非您有任何配置的区域,否则您应该能够访问您的操作 localhost/My/Indexlocalhost/My如果您有默认路由并将操作作为索引。

该错误还会提供ASP.NET正在搜索以查找视图的区域列表。

0

创建自定义路线并将其放在默认路线之前。

这样的:

// ... 
public class RouteConfig 
{ 
    public static void RegisterRoutes(RouteCollection routes) 
    { 
    routes.MapRoute(
      name: "me", 
      url: "me/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
      routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "My", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 
} 
// ... 

希望这个作品......

相关问题