【发布时间】:2013-06-05 13:41:17
【问题描述】:
我有一个包含 4 个项目的解决方案:
-
AS.Core.Common(参考:数据、网络) -
AS.Core.Controllers(参考:Common、Data、Web) AS.Core.Data-
AS.Core.Web(参考:数据)
在我的控制器中,我返回视图的任何地方:
return View("~/Views/Home/Index.cshtml");
它以红色突出显示,我得到一个"Cannot resolve view '~/Views/Home/Index.cshtml'"。
所有四个项目都成功构建,但是当我按下 F5 时,我得到以下信息:
“/”应用程序中的服务器错误。
找不到资源。
描述:HTTP 404。您正在寻找的资源(或其之一 依赖项)可能已被删除,名称已更改,或者是 暂时不可用。请查看以下 URL 并制作 确保拼写正确。
请求的网址:/
版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.17929
如何让我的控制器看到我的视图以便正确解析?我假设这就是我收到 404 响应的原因。
我的Global.asax.cs 看起来像这样:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
// Register the default hubs route: ~/signalr
RouteTable.Routes.MapHubs();
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
我的RouteConfig.cs(我添加了命名空间,但似乎没有帮助)
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
namespaces: new[] { "AS.Core.Controllers" },
defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
}
这是我的HomeController.cs
public class HomeController : Controller
{
public ActionResult Index()
{
//return View("~/Views/Home/Index.cshtml");
return View();
}
}
【问题讨论】:
-
问题可能不在于找到视图。找不到视图会呈现另一个更具体的错误,而不是 404。您的错误可能与路由有关。 global.asax 中的路由表怎么样?
-
我在原始帖子中添加了路由。
-
抱歉不够清楚:) RouteConfig.RegisterRoutes(RouteTable.Routes) 方法中的内容。
-
我猜你没有 HomeController.cs 包含一个名为 HomeController 的类和一个名为 Index() 的方法??
-
刚刚看到(在其他评论中)您的控制器在其他项目中,对吧?那么您可能应该将 namespaces: new string[]{"YourControllerProjsNameSpace"} 添加到您的 MapRoute() 中。
标签: asp.net-mvc asp.net-mvc-4 controller