代码之家  ›  专栏  ›  技术社区  ›  Mouadh

ASP.NET MVC-视图位置问题:找不到视图“Index”或其主视图

  •  3
  • Mouadh  · 技术社区  · 16 年前

    我被要求将这个项目集成到经典asp.net中的现有web项目中

    (我添加了包含源代码的文件夹,但没有创建新项目,因为它们必须在同一个项目中)

    我已经配置了我的 web.config 文件

    <pages pageBaseType="System.Web.UI.Page" >
          <controls>
            <add tagPrefix="asp"
                 namespace="System.Web.UI"
                 assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add tagPrefix="asp"
                 namespace="System.Web.UI.WebControls"
                 assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
          </controls>
          <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
            <add namespace="System.Linq" />
            <add namespace="System.Collections.Generic" />
            <add namespace="SearchApp.Helpers.CheckBoxList"/>
            <add namespace="SearchApp.Helpers.Pager"/>
          </namespaces>
        </pages>
        <httpModules>
          <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
          <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"  />
        </httpModules>
        <!--- END MVC config section-->
    

    还有我的 global.asax

    public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");           
    
                routes.MapRoute(
                    "SearchApp", // Route name
                    "SearchApp/{controller}.aspx/{action}/{id}", // URL with parameters
                    new { controller = "Search", action = "Index", id = "" } // Parameter defaults
                );
            }
    
    protected void Application_Start(Object sender, EventArgs e)
    {
                AreaRegistration.RegisterAllAreas();
    
                RegisterRoutes(RouteTable.Routes);
    }
    

    项目的层次结构是(我找不到如何在文章中插入图像):

     +Project
     ++MVC2AppFolder
     +++Controller
     ++++SearchController.cs
     +++Views
     ++++Search
     +++++Index.aspx
     ++global.asax
     ++web.config
    

    你可以看到 不在MVC应用程序文件夹中

    http://localhost/cstfwsrv/SearchApp/Search.aspx/Index[ ^]出现以下错误:

    The view `Index` or its master was not found. The following locations were searched:
    ~/Views/Search/Index.aspx
    ~/Views/Search/Index.ascx
    ~/Views/Shared/Index.aspx
    ~/Views/Shared/Index.ascx
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
    Exception Details: System.InvalidOperationException: The view 'Index' or its master was not found. The following locations were searched:
    ~/Views/Search/Index.aspx
    ~/Views/Search/Index.ascx
    ~/Views/Shared/Index.aspx
    ~/Views/Shared/Index.ascx
    
    Source Error:
    
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 
    
    Stack Trace:
    
    
    [InvalidOperationException: The view 'Index' or its master was not found. The following locations were searched:
    ~/Views/Search/Index.aspx
    ~/Views/Search/Index.ascx
    ~/Views/Shared/Index.aspx
    ~/Views/Shared/Index.ascx]
       System.Web.Mvc.ViewResult.FindView(ControllerContext context) +253553
       System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +139
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +10
       System.Web.Mvc.<>c__DisplayClass14.<InvokeActionResultWithFilters>b__11() +20
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +251
       System.Web.Mvc.<>c__DisplayClass16.<InvokeActionResultWithFilters>b__13() +19
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +178
       System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +314
       System.Web.Mvc.Controller.ExecuteCore() +105
       System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +39
       System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
       System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +34
       System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
       System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
       System.Web.Mvc.Async.WrappedAsyncResult`1.End() +59
       System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +44
       System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +7
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8677678
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
    

    如您所见,他在下面搜索Index.aspx页面 ~/Views/Search/Index.aspx ~/MVC2AppFolder/Views/Search/Index.aspx

    有没有办法在Route方法或其他任何地方设置视图路径?

    1 回复  |  直到 11 年前
        1
  •  2
  •   hackerhasid    16 年前

    您可以创建自己的ViewEngine,它将在正确的位置查找您的视图。基本上就是预先准备好的 ~/MVC2AppFolder/{0} 在查看路径之前。

    http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx

    推荐文章