呈现与父视图相同的部分名称 - 崩溃WebDev.WebServer40.exe

Jam*_*xon 2 asp.net-mvc-2

我想知道其他人是否也有同样的问题或是否只是我!

鉴于我有一个视图Purchases.aspx和一个局部视图Purchases.ascx:

Purchases.aspx如果我这样做:Html.RenderPartial("Purchases")那么WebDev.WebServer40.exe基本关闭.

我猜这是由Stack Overflow引起的,因为RenderPartial无法确定它应该呈现什么(.aspx或.ascx).

这是一个错误,它是一个定义的行为,还是只是发生在我身上?

Ahm*_*mad 7

它是定义的行为,因为ViewLocationFormats和PartialViewLocationFormats定义如下,并且将首先查看aspx页面.

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

PartialViewLocationFormats = ViewLocationFormats;
Run Code Online (Sandbox Code Playgroud)

在我看来,PartialViewLocationFormats应该排除aspx定义.覆盖默认的WebFormViewengine可以解决此问题.注意,您需要在Application_Start()方法中注册它

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",
                    };
    }
}
Run Code Online (Sandbox Code Playgroud)