子文件夹

时间:2015-11-28 12:27:45

标签: asp.net-mvc razor asp.net-mvc-routing

我需要一些路由帮助。我试图找到现有的问题 - 答案,但没有任何帮助我。如果已经有答案,请链接。

我的结构:

/Controller
--/Root
----/HomeController.cs
/Views
--/Root
----/Home
------/Index.cshtml

问题是,无法找到Index.cshtml。

  

视图'索引'或者找不到它的主人。以下地点   被搜查了:

     

〜/查看/主页/的Index.aspx
  〜/浏览/首页/ Index.ascx
  〜/ Views / Shared / Index.aspx
  〜/查看/共享/ Index.ascx
  〜/ Views / Home / Index.cshtml
  〜/浏览/首页/ Index.vbhtml
  〜/ Views / Shared / Index.cshtml
  〜/ Views / Shared / Index.vbhtml

堆栈跟踪:

  

System.Web.Mvc.ViewResult.FindView(ControllerContext context)+382   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)+116   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext,ActionResult actionResult)+13   System.Web.Mvc。&lt;&gt; c__DisplayClass1a.b__17()+23   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter过滤器,ResultExecutingContext preContext,Func 1 continuation) +245<br /> System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +22 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList 1过滤器,ActionResult actionResult)+176      System.Web.Mvc.Async。&lt;&gt; c__DisplayClass2a.b__20()+75      System.Web.Mvc.Async。&lt;&gt; c__DisplayClass25.b__22(IAsyncResult asyncResult)+99      System.Web.Mvc.Async.WrappedAsyncResult 1.End() +50 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +16 System.Web.Mvc.Async.WrappedAsyncResult 1.End()+50      System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+36      System.Web.Mvc.Async。&lt;&gt; c__DisplayClass4.b__3(IAsyncResult ar)+16      System.Web.Mvc.Async.WrappedAsyncResult 1.End() +50 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +16 System.Web.Mvc.Async.WrappedAsyncResult 1.End()+50      System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+28   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)+9   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+9721605      System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155

路由是这样的:

routes.MapRoute(
    name: "Home",
    url: "Root/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    ).DataTokens.Add("app", "Home");

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

默认情况下,MVC在~/Views/YourControllerName文件夹中查找视图。看起来您的控制器和视图有一个名为root的目录。我不确定你为什么要那样做。如果您这样做是为了在项目中逻辑地组织代码,您应该考虑Areas

您可以创建一个名为root的新区域。这将在~/Areas下创建一个名为root的目录。您可以在其下创建控制器和视图。当您定义路线时,您不需要在您的网址中包含单词Root,因为您将有一个区域注册以单词Root(您可以自定义)开始

但是,如果您真的不想使用区域,但希望将视图保留在~/Views/root目录下,则可以创建自定义视图引擎并指定查找视图文件的位置。

public class MyCustomViewEngine : RazorViewEngine
{
    public MyCustomViewEngine()
    {
        string[] viewLocationFormatArr=new string[4];
        viewLocationFormatArr[0] = "~/Views/root/{1}/{0}.cshtml";
        viewLocationFormatArr[1] = "~/Views/Root/{1}/{0}.vbhtml";
        viewLocationFormatArr[2] = "~/Views/Shared/{1}/{0}.vbhtml";
        viewLocationFormatArr[3] = "~/Views/Shared/{1}/{0}.vbhtml";
        this.ViewLocationFormats = viewLocationFormatArr;

        string[] masterLocationFormatArr = new string[4];
        masterLocationFormatArr[0] = "~/Views/root/{1}/{0}.cshtml";
        masterLocationFormatArr[1] = "~/Views/root/{1}/{0}.vbhtml";
        masterLocationFormatArr[2] = "~/Views/Shared/{1}/{0}.vbhtml";
        masterLocationFormatArr[3] = "~/Views/Shared/{1}/{0}.vbhtml";
        this.MasterLocationFormats = masterLocationFormatArr;

        string[] partialViewLocationFormatArr = new string[4];
        partialViewLocationFormatArr[0] = "~/Views/root/{1}/{0}.cshtml";
        partialViewLocationFormatArr[1] = "~/Views/root/{1}/{0}.vbhtml";
        partialViewLocationFormatArr[2] = "~/Views/Shared/{1}/{0}.vbhtml";
        partialViewLocationFormatArr[3] = "~/Views/Shared/{1}/{0}.vbhtml";
        this.ViewLocationFormats = partialViewLocationFormatArr;

    }
}

确保在Application_Start事件中注册此自定义视图引擎。

ViewEngines.Engines.Clear();
var ourViewEngine = new MyCustomViewEngine();
ViewEngines.Engines.Add(ourViewEngine);

有关从自定义位置呈现视图的详细博客是here