2015-02-24 58 views
1

我有一个名为fafa我有一些控制器内的area,你可以在这里看到他们中的一个:无法找到资源发生在MVC而控制器存在

namespace UI.Areas.fa.Controllers 
    { 
     public class NewsController : Controller 
     { 
      // 
      // GET: /fa/News/ 

      private NewsRepository NewsRepository = new NewsRepository(); 
      private GroupRepository GroupRepository = new GroupRepository(); 
      public ActionResult Index(int? groupId, int? num) 
      { 
       ViewBag.slideshowNews = NewsRepository.FindBy(i => i.Group.LanguageType == "Persian" && i.Group.Type == "News" && i.ShowOnSlide == "Yes").OrderByDescending(i => i.Id).Take(6).ToList(); 
       ViewBag.slideshowNewsNav = NewsRepository.FindBy(i => i.Group.LanguageType == "Persian" && i.Group.Type == "News" && i.ShowOnSlide == "Yes").OrderBy(i => i.Id).Take(6).ToList(); 
       ViewBag.threetopnews = NewsRepository.FindBy(i => i.Group.LanguageType == "Persian" && i.Group.Type == "News").OrderByDescending(i => i.Id).Take(3).ToList(); 
       ViewBag.newsSubGroup = GroupRepository.FindBy(i => i.Type == "News" && i.GroupId != null && i.LanguageType == "Persian").OrderByDescending(i => i.Id).Take(6).ToList(); 
       ViewBag.morenewsSubGroup = GroupRepository.FindBy(i => i.Type == "News" && i.GroupId != null && i.LanguageType == "Persian").OrderByDescending(i => i.Id).Skip(6).ToList(); 
       int realnum = 1; 
       if (num.HasValue) realnum = num.Value; 
       if (groupId.HasValue) 
       { 
        ViewBag.news = 
         NewsRepository.FindBy(i => i.Group.LanguageType == "Persian" && i.GroupId == groupId).Take(realnum * 8) 
             .OrderByDescending(i => i.Id) 
             .ToList(); 
       } 
       else 
       { 
        ViewBag.news = 
         NewsRepository.FindBy(i => i.Group.LanguageType == "Persian" && i.Group.Type == "News").Take(realnum * 8) 
            .OrderByDescending(i => i.Id) 
            .ToList(); 
       } 
       return View(); 
      } 
      public ActionResult ShowDetails(int id, int group, int? num) 
      { 
       ViewBag.newsDetails = NewsRepository.FindBy(i => i.Id == id).First(); 
       Group objGroup = GroupRepository.FindBy(i => i.Id == group).First(); 
       ViewBag.Groups = GroupRepository.FindBy(i => i.GroupId == objGroup.GroupId && i.LanguageType == "Persian").OrderByDescending(i => i.Id).Take(6).ToList(); 
       ViewBag.moreGroup = GroupRepository.FindBy(i => i.GroupId == objGroup.GroupId && i.LanguageType == "Persian").OrderByDescending(i => i.Id).Skip(6).ToList(); 
       int realnum = 1; 
       if (num.HasValue) realnum = num.Value; 

       ViewBag.news = 
        NewsRepository.FindBy(i => i.Group.LanguageType == "Persian" && i.GroupId == group).Take(realnum * 8) 
            .OrderByDescending(i => i.Id) 
            .ToList(); 

       return View(); 
      } 

     } 
    } 

但是,当我把这个urlhttp://localhost:17442/fa/News,这个错误发生

The resource cannot be found. 

我的全局代码:

protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 

      WebApiConfig.Register(GlobalConfiguration.Configuration); 
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 
      BundleConfig.RegisterBundles(BundleTable.Bundles); 
     } 

区登记:

public class faAreaRegistration : AreaRegistration 
    { 
     public override string AreaName 
     { 
      get 
      { 
       return "fa"; 
      } 
     } 

     public override void RegisterArea(AreaRegistrationContext context) 
     { 
      context.MapRoute(
       "fa_default", 
       "fa/{controller}/{action}/{id}", 
       new { action = "Index", id = UrlParameter.Optional } 
      ); 
     } 
    } 

routedebugger结果:enter image description here

我应该检查哪些部位?

+1

假设你在'global.asax'的'Application_Start'方法内调用'AreaRegistration.RegisterAllAreas();'是安全的吗? – cgijbels 2015-02-24 14:30:53

+1

您还需要一个注册该区域的类,以便它可以通过'AreaRegistration.RegisterAllAreas()'获取,通常如果您使用VS并添加新区域,则会自动生成“faAreaRegistration”类。你也有这个吗? – cgijbels 2015-02-24 14:32:44

+1

如果你添加一个操作方法'public ActionResult Test(){return Content(“inside test”); }'并且导航到'http:// localhost:17442/fa/News/Test',你是否收到“内部测试”消息或资源未找到? – cgijbels 2015-02-24 14:39:27

回答

2

路线调试器说,它正在寻找命名空间ExportVision.Areas.fa.*,但是你的控制器处于UI.Areas.fa.*命名空间。

要么将​​控制器的命名空间更改为ExportVision.Areas.fa.Controllers,要么显式地将自定义命名空间包含在区域路径中。

public override void RegisterArea(AreaRegistrationContext context) 
{ 
    context.MapRoute(
     "fa_default", 
     "fa/{controller}/{action}/{id}", 
     new { controller = "News", action = "Index", id = UrlParameter.Optional }, 
     namespaces: new[] { "UI.Areas.fa.Controllers" } 
    ); 
} 
1

尝试增加命名空间RegisterArea方法:

public override void RegisterArea(AreaRegistrationContext context) 
{ 
    context.MapRoute(
     "fa_default", 
     "fa/{controller}/{action}/{id}", 
     new { action = "Index", id = UrlParameter.Optional }, 
     namespaces: new[] { "UI.Areas.fa.Controllers" } 
    ); 
}