C# 当控制器存在时,在MVC中找不到资源
我在C# 当控制器存在时,在MVC中找不到资源,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,我在fa内部有一个名为fa的区域我有一些控制器,您可以在这里看到其中一个: namespace UI.Areas.fa.Controllers { public class NewsController : Controller { // // GET: /fa/News/ private NewsRepository NewsRepository = new NewsRepos
fa
内部有一个名为fa
的区域
我有一些控制器,您可以在这里看到其中一个:
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();
}
}
}
但是当我调用这个url
http://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
结果:
我应该检查哪些部分?尝试将命名空间添加到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" }
);
}
路由调试器说它正在查找名称空间
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" }
);
}
可以安全地假设您有一个对
AreaRegistration.RegisteralAreas()的调用
在全局.asax的应用程序\u Start
方法中,您还需要一个注册区域的类,以便该区域被区域注册。registeralareas()
,如果您使用VS并添加新区域,通常会自动生成faAreaRegistration
类。如果您添加一个操作方法public ActionResult Test(){return Content(“inside Test”);}
并导航到http://localhost:17442/fa/News/Test
,您是否收到“内部测试”消息或未找到资源?好的,看来您的路线注册有问题,现在我们可以来回尝试猜测问题是什么,但我认为如果您安装Phil Haack的RoutedBugger
NuGet软件包会更容易,它非常轻量级,一旦您通过上述URL拨打电话,您将在页面底部获得所有已注册路由的列表,希望这能让你知道为什么你的路线找不到。如果没有,请毫不犹豫地将该列表粘贴到您的问题中,这样我们就可以看一看。@danludwig yep,的确,很好的回答!我把重点放在路线确实被找到这一事实上;-)