Render Partial of same name as parent View - Crashes WebDev.WebServer40.exe

柔情痞子 提交于 2019-11-30 19:10:08

问题


I'm wondering whether other people are having this same issue or whether it's just me !

Given I have a View Purchases.aspx and a partial view Purchases.ascx:

Within Purchases.aspx if I do: Html.RenderPartial("Purchases") then WebDev.WebServer40.exe basically closes.

I'm guessing that this is caused by a Stack Overflow because RenderPartial cannot determine what it's supposed to render (.aspx or .ascx).

Is this a bug, is it a defined behaviour, or is it just happening for me?


回答1:


It is defined behaviour since the ViewLocationFormats and PartialViewLocationFormats are defined as follows and an aspx page will be be looked at first.

ViewLocationFormats = new[] {
            "~/Views/{1}/{0}.aspx",
            "~/Views/{1}/{0}.ascx",
            "~/Views/Shared/{0}.aspx",
            "~/Views/Shared/{0}.ascx"
        }; 

PartialViewLocationFormats = ViewLocationFormats;

PartialViewLocationFormats should exclude the aspx definitions in my opinion. Overriding the default WebFormViewengine can resolve this. Note, you will need to register this in the Application_Start() method

public class ASPXViewEngine: WebFormViewEngine
{
    public ASPXViewEngine()
    {
        base.PartialViewLocationFormats =
                new string[]
                    {
                        "~/Views/{1}/{0}.ascx",
                        "~/Views/Shared/{0}.ascx"
                    };

        base.AreaPartialViewLocationFormats =
                new string[]
                    {
                        "~/Areas/{2}/Views/{1}/{0}.ascx",
                        "~/Areas/{2}/Views/Shared/{0}.ascx",
                    };
    }
}


来源:https://stackoverflow.com/questions/3363847/render-partial-of-same-name-as-parent-view-crashes-webdev-webserver40-exe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!