未找到“搜索”视图或其主视图,或者视图引擎不支持搜索的位置

时间:2018-02-16 20:58:41

标签: c# asp.net-mvc-views

    public ActionResult Search(string name)
    {

        if (string.IsNullOrWhiteSpace(name))
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var movie = db.Movies.Where(x => x.Title.Contains(name)).FirstOrDefault();
        if (movie == null)
        {
            return HttpNotFound();
        }
        return View(movie);
    }

我正在尝试创建一个搜索参数来从我在c#中创建的表中提取数据。运行应用程序将我带到localhost:xxxx,它向我显示整个表。我想localhost:xxxx / movies / search / man用它里面的字符串来拉我的数据。

这是RouteConfig.cs

routes.MapRoute(
   name: "Search",
   url: "movies/search/{name}",
   defaults: new { controller = "Movies", action = "Search", name = UrlParameter.Optional }
);

调用堆栈:

  [InvalidOperationException: The view 'Search' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Movies/Search.aspx
~/Views/Movies/Search.ascx
~/Views/Shared/Search.aspx
~/Views/Shared/Search.ascx
~/Views/Movies/Search.cshtml
~/Views/Movies/Search.vbhtml
~/Views/Shared/Search.cshtml
~/Views/Shared/Search.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.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   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() +9748665
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159

2 个答案:

答案 0 :(得分:1)

好像你没有看到这个要渲染的视图。

添加视图(假设你使用ASP-MVC进入你的视图文件夹,添加一个“Movies”文件夹(如果不存在)并在该文件夹下添加一个Search.cshtml文件,这是一个例子: Adding Search view in vs

这个答案是对你提供的堆栈跟踪的响应,这似乎与标题无关 - 读取堆栈跟踪似乎Linq不是问题。

希望这有帮助

答案 1 :(得分:0)

公共ActionResult搜索(字符串名称)         {

        if (string.IsNullOrWhiteSpace(name))
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var movies = db.Movies.Where(x => x.Title.Contains(name)).ToList();
        if (movies == null)
        {
            return HttpNotFound();
        }
        return View("index", movies);
    }

感谢您的见解。这是一个问题。我没有指向任何东西。出于我的目的,我只是回顾了我用作索引的内容。