Render partial from different folder (not shared)

前端 未结 10 1923
名媛妹妹
名媛妹妹 2020-11-29 17:12

How can I have a view render a partial (user control) from a different folder? With preview 3 I used to call RenderUserControl with the complete path, but whith upgrading to

相关标签:
10条回答
  • 2020-11-29 17:13

    I've created a workaround that seems to be working pretty well. I found the need to switch to the context of a different controller for action name lookup, view lookup, etc. To implement this, I created a new extension method for HtmlHelper:

    public static IDisposable ControllerContextRegion(
        this HtmlHelper html, 
        string controllerName)
    {
        return new ControllerContextRegion(html.ViewContext.RouteData, controllerName);
    }
    

    ControllerContextRegion is defined as:

    internal class ControllerContextRegion : IDisposable
    {
        private readonly RouteData routeData;
        private readonly string previousControllerName;
    
        public ControllerContextRegion(RouteData routeData, string controllerName)
        {
            this.routeData = routeData;
            this.previousControllerName = routeData.GetRequiredString("controller");
            this.SetControllerName(controllerName);
        }
    
        public void Dispose()
        {
            this.SetControllerName(this.previousControllerName);
        }
    
        private void SetControllerName(string controllerName)
        {
            this.routeData.Values["controller"] = controllerName;
        }
    }
    

    The way this is used within a view is as follows:

    @using (Html.ControllerContextRegion("Foo")) {
        // Html.Action, Html.Partial, etc. now looks things up as though
        // FooController was our controller.
    }
    

    There may be unwanted side effects for this if your code requires the controller route component to not change, but in our code so far, there doesn't seem to be any negatives to this approach.

    0 讨论(0)
  • 2020-11-29 17:13

    The VirtualPathProviderViewEngine, on which the WebFormsViewEngine is based, is supposed to support the "~" and "/" characters at the front of the path so your examples above should work.

    I noticed your examples use the path "~/Account/myPartial.ascx", but you mentioned that your user control is in the Views/Account folder. Have you tried

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

    or is that just a typo in your question?

    0 讨论(0)
  • 2020-11-29 17:16

    you should try this

    ~/Views/Shared/parts/UMFview.ascx
    

    place the ~/Views/ before your code

    0 讨论(0)
  • 2020-11-29 17:20

    For a user control named myPartial.ascx located at Views/Account folder write like this:

    <%Html.RenderPartial("~/Views/Account/myPartial.ascx");%>
    
    0 讨论(0)
  • 2020-11-29 17:27

    For readers using ASP.NET Core 2.1 or later and wanting to use Partial Tag Helper syntax, try this:

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

    The tilde (~) is optional.

    The information at https://docs.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-3.1#partial-tag-helper is helpful too.

    0 讨论(0)
  • 2020-11-29 17:28

    In my case I was using MvcMailer (https://github.com/smsohan/MvcMailer) and wanted to access a partial view from another folder, that wasn't in "Shared." The above solutions didn't work, but using a relative path did.

    @Html.Partial("../MyViewFolder/Partials/_PartialView", Model.MyObject)
    
    0 讨论(0)
提交回复
热议问题