asp.net-mvc 未找到局部视图或没有视图引擎支持搜索的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22054264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:33:46  来源:igfitidea点击:

partial view was not found or no view engine supports the searched locations

asp.net-mvcasp.net-mvc-3razor

提问by siva

I have following controller code:

我有以下控制器代码:

public MyController:Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [ChildActionOnly]
    public ActionResult MyPartialViewAction()
    {
        return PartialView("~/Views/Shared/MyCustomFolder/_MyPartialView",PartialViewModel);
    }
}

and my Index view has the following code :

我的索引视图具有以下代码:

@HTML.Action("MyPartialViewAction")

When I run the Web app I get HttpException with InnerExceptionMessage as :

当我运行 Web 应用程序时,我收到 HttpException 和 InnerExceptionMessage 为:

InnerException {"The partial view '~/Views/Shared/MyCustomFolder/_MyPartialView' was not found or no view engine supports the searched locations. The following locations were searched:\r\n~~/Views/Shared/MyCustomFolder/_MyPartialView"} System.Exception {System.InvalidOperationException}

InnerException {"未找到局部视图 '~/Views/Shared/MyCustomFolder/_MyPartialView' 或没有视图引擎支持搜索的位置。搜索了以下位置:\r\n~~/Views/Shared/MyCustomFolder/_MyPartialView" } System.Exception {System.InvalidOperationException}

What I have tried till now :

到目前为止我尝试过的:

  • Tried moving _MyPartialViewfrom ~/Views/Shared/MyCustomFolderto ~/Views/Shared/and ~/Views/MyControllerFolderbut still error exists

  • Tried changing my Index View code to @HTML.RenderAction()but no luck.

  • 尝试_MyPartialView~/Views/Shared/MyCustomFolderto移动~/Views/Shared/~/Views/MyControllerFolder但仍然存在错误

  • 尝试将我的索引视图代码更改为@HTML.RenderAction()但没有运气。

Any inputs on where I'm going wrong ?

关于我哪里出错的任何输入?

Thanks

谢谢

回答by Maess

You need to add the .cshtml extension to the view name:

您需要将 .cshtml 扩展名添加到视图名称中:

return PartialView("~/Views/Shared/MyCustomFolder/_MyPartialView.cshtml",PartialViewModel);

回答by Sanvir Manilal

Just in case the selected answer is not working for you:

以防万一所选答案不适合您:

I changed the build action of the cshtml file to Content and it fixed it.

我将 cshtml 文件的构建操作更改为 Content 并修复了它。

回答by lukejkw

If the answers are still not working for you and you have put your partial in a nested folder inside of partials, ensure that you provide the full path to the partial with the extension.

如果答案仍然不适合您,并且您已将部分放在部分内的嵌套文件夹中,请确保提供带有扩展名的部分的完整路径。

@Html.Partial("~/Views/Partials/Components/_AdvancedComponentRenderer.cshtml", Model.Content);

@Html.Partial("~/Views/Partials/Components/_AdvancedComponentRenderer.cshtml", Model.Content);

回答by Michael Sefranek

Also double check for spaces after your filename, before the ".cshtml". I had inserted one blank space accidentally after my filename, and this disrupted the automatic behavior of PartialView() for me.

还要仔细检查文件名之后的空格,在“.cshtml”之前。我在文件名后不小心插入了一个空格,这对我来说破坏了 PartialView() 的自动行为。

回答by Nitika Chopra

Hope, this code helps you (In ".cshtml"file)

希望,此代码可以帮助您(在“.cshtml”文件中)

@using Microsoft.AspNetCore.Mvc.ViewEngines
@inject ICompositeViewEngine Engine

....
string path = "../Themes/User/_head";

@if (Engine.FindView(ViewContext, path, isMainPage: false).Success)
{
    <partial name="@path" for="@Model" />
}
else
{
    <partial name="../Themes/Admin/_head" />
}

Thanks

谢谢

回答by dwilli

I got this error message because the partial view was added to the project file (.csproj) but was not actually present on disk.

我收到此错误消息是因为部分视图已添加到项目文件 (.csproj) 但实际上并未出现在磁盘上。