我如何能有一个视图呈现部分(用户控件)从不同的文件夹? 有了预览3,我习惯用完整的路径调用RenderUserControl,但升级到预览5,这是不可能的了。 相反,我们得到了RenderPartial方法,但它没有提供我所寻找的功能。


当前回答

对于使用ASP的读者。NET Core 2.1或更高版本,想要使用Partial Tag Helper语法,试试这个:

<partial name="~/Views/Folder/_PartialName.cshtml" />

波浪号(~)是可选的。

https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-3.1#partial-tag-helper上的信息也很有用。

其他回答

对于使用ASP的读者。NET Core 2.1或更高版本,想要使用Partial Tag Helper语法,试试这个:

<partial name="~/Views/Folder/_PartialName.cshtml" />

波浪号(~)是可选的。

https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-3.1#partial-tag-helper上的信息也很有用。

对于名为myPartial的用户控件。ascx位于Views/Account文件夹,如下所示:

<%Html.RenderPartial("~/Views/Account/myPartial.ascx");%>

你应该试试这个

~/Views/Shared/parts/UMFview.ascx

将~/Views/放在代码之前

只需要包含视图的路径和文件扩展名。

剃须刀:

@Html.Partial("~/Views/AnotherFolder/Messages.cshtml", ViewData.Model.Successes)

ASP。NET引擎:

<% Html.RenderPartial("~/Views/AnotherFolder/Messages.ascx", ViewData.Model.Successes); %>

如果这不是你的问题,你能包括你的代码,用来与RenderUserControl工作吗?

VirtualPathProviderViewEngine是WebFormsViewEngine的基础,它应该支持路径前面的“~”和“/”字符,所以上面的例子应该可以工作。

我注意到你的例子使用了路径“~/Account/myPartial”。ascx”,但是您提到您的用户控件在Views/Account文件夹中。你试过了吗

< %。RenderPartial(“~点击量/ Html / myPartial . ascx”);% >账户

还是你的问题打错了?