本文介绍了不能调试通过自定义加载EmbeddedResource意见的VirtualPathProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我(这里源

编辑:

我一直在尝试通过VPP的加载的ASPX页面,并通过查看编译源(使用<一个href=\"http://blogs.msdn.com/b/davidebb/archive/2010/02/03/debugging-asp-net-generated-$c$c.aspx\">David Ebbo的技术)和行杂都像这样产生的:

  275线:1的#lineHTTP://server/EmbeddedPage.aspx
线276:this.InitializeCulture();

通常情况下,这些线沿线的产生

  275线:1的#lineD:/somesln/someproj/EmbeddedPage.aspx

不知道是否可以帮助任何人,或不...

编辑2:

在大卫给我发了他的code,我做了一些进一步的调查和下面的事情似乎是正确的:


  1. 您不能在的.aspx除非System.Web程序引用(在VS 2010中)设置一个断点

  2. 如果你创建一个最小的.aspx页面指令&LT;%@页面语言=C#%&GT; 和设置一个断点,VS会停在源文件中的断点


  3. 如果你创建一个非最小的.aspx与指令&LT;%@页面语言=C#AutoEventWireup =真codeBehind =WebForm1.aspx.cs中继承=VppDebugTest.WebForm1%GT; 键,设置断点,查看VS会带你到dissasembly调试视图时


--- HTTP://server/WebForm1.aspx ----------------------------- -------------------
0000003a MOV ECX,DWORD PTR [EBP-3Ch]
0000003d调用63EC54F0
00000042 MOV DWORD PTR [EBP-44H],EAX
00000045 MOV EDX,DWORD PTR DS:[03E62200h]
0000004b MOV ECX,DWORD PTR [EBP-44H]

它仍然不会停止在任何断点在剃刀的看法,这是不幸的是我真正需要的是能够做到!这东西的.aspx可能是一个红色的鲱鱼。

编辑:

5:如果我把调用Debugger.Break()到我Index.cshtml,调试器停在反汇编视图,并且没有编译指示在所有的,不正确的或以其他方式


  1. 如果我手动编写 @ {#行1C:\\用户\\哈利\\桌面\\ VppDebugTest \\ VppDebugTest.Views \\查看\\首页\\ Index.cshtml} 在我看来,调试好的将停止在文件中。所以,也许解决方案是我的VPP插入的#line编译指示到CSHTML文件本身?


解决方案

我有同样的问题,终于得到它通过使用自定义RazorHost工作。看来,物理文件位置使用不为嵌入文件返回正确结果 HostingEnvironment.MapPath()方法解决了。

我做了什么:

 公共类MyCustomRazorHostFactory:WebRazorHostFactory
{
    公众覆盖System.Web.WebPages.Razor.WebPageRazorHost CreateHost(字符串virtualPath,串physicalPath)
    {
        //实现从MvcRazorHostFactory被盗:)
        VAR主机= base.CreateHost(virtualPath,physicalPath);        如果(!host.IsSpecialPage)
        {
            返回新MyCustomRazorHost(virtualPath,physicalPath);
        }        返回主机;
    }
}公共类MyCustomRazorHost:MvcWebPageRazorHost
{
    公共MyCustomRazorHost(字符串virtualPath,串physicalPath)
        :基地(virtualPath,physicalPath)
    {
        如果(MyMagicHelper.IsEmbeddedFile(virtualPath))
        {
            PhysicalPath = MyMagicHelper.GetPhysicalFilePath(virtualPath);
        }
    }
}//简化演示目的
公共静态类MyMagicHelper
{
    公共静态布尔IsEmbeddedFile(字符串virtualPath)
    {
        // ...检查路径是一个嵌入的文件路径
    }    公共静态字符串GetPhysicalFilePath(字符串virtualPath)
    {
        // ...解决虚拟文件并返回正确的物理文件路径
    }
}

当你需要告诉ASP.NET,应该使用的主机厂家最后一步。这是在web.config中完成的:

 &LT; system.web.webPages.razor&GT;
    &LT;主机factoryType =My.Custom.Namespace.MyCustomRazorHostFactory/&GT;
&LT; /system.web.webPages.razor>

我知道我的回答来得有点迟,但希望别人能过这个问题绊脚石,像我一样的时候使用它。 :)

I have written a custom VirtualPathProvider (source here) which will return content from EmbeddedResources, or from the original file if it has been told where to find it (this allows you to edit and update the files without having to rebuild). This is working fine, so far.

What isn't working is debugging. If I add a breakpoint to the view, it doesn't load the symbols. I can see why this is difficult (how can the ASP compiler know where the source file is, in order to spot the breakpoints?), but am looking for a way to hint to the compiler where the source file can be found.

Example project here: http://dl.dropbox.com/u/2808109/VppDebugTest.zip

edit:

I've been experimenting with an ASPX page loaded via the VPP, and by viewing the Compiled Source (using David Ebbo's technique), and the line pragmas are generated like so:

Line 275:              #line 1 "http://server/EmbeddedPage.aspx"
Line 276:              this.InitializeCulture();

Normally, these are generated along the lines of

Line 275:              #line 1 "d:/somesln/someproj/EmbeddedPage.aspx"

Don't know if that helps anyone, or not...

edit 2:

After David sent me his code, I have done some further investigation and the following things seem to be true:

  1. you can't set a breakpoint in a .aspx unless system.web is referenced (in VS 2010)
  2. if you create a minimal .aspx page with the directives <%@ Page Language="C#" %> and set a breakpoint, VS will stop at the breakpoint in the source file

  3. if you create a non minimal .aspx with directives <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="VppDebugTest.WebForm1" %> and set a breakpoint, when viewed VS will take you to the dissasembly debug view

--- http://server/WebForm1.aspx ------------------------------------------------0000003a mov ecx,dword ptr [ebp-3Ch] 0000003d call 63EC54F0 00000042 mov dword ptr [ebp-44h],eax 00000045 mov edx,dword ptr ds:[03E62200h] 0000004b mov ecx,dword ptr [ebp-44h]

It still wont stop at any breakpoints in the Razor views, which is unfortunately what I really need to be able to do! This .aspx stuff may be a red herring.

edit:

5: If I put a call to Debugger.Break() into my Index.cshtml, the debugger stops at disassembly view, and there are no pragmas at all, incorrect or otherwise

  1. If I manually write @{ #line 1 "C:\Users\Harry\Desktop\VppDebugTest\VppDebugTest.Views\Views\Home\Index.cshtml" } in my view, the debugged will stop in the file. So maybe the solution is for my VPP to insert the #line pragmas into the cshtml files itself??
解决方案

I had the same problem and finally got it working by using a custom RazorHost. It seems that the physical file location is resolved using the HostingEnvironment.MapPath() method which does not return the correct result for embedded files.

What I did:

public class MyCustomRazorHostFactory : WebRazorHostFactory
{
    public override System.Web.WebPages.Razor.WebPageRazorHost CreateHost( string virtualPath, string physicalPath )
    {
        // Implementation stolen from MvcRazorHostFactory :)
        var host = base.CreateHost( virtualPath, physicalPath );

        if( !host.IsSpecialPage )
        {
            return new MyCustomRazorHost( virtualPath, physicalPath );
        }

        return host;
    }
}

public class MyCustomRazorHost : MvcWebPageRazorHost
{
    public MyCustomRazorHost( string virtualPath, string physicalPath )
        : base( virtualPath, physicalPath )
    {
        if( MyMagicHelper.IsEmbeddedFile( virtualPath ) )
        {
            PhysicalPath = MyMagicHelper.GetPhysicalFilePath(virtualPath);
        }
    }
}

// Simplified for demonstration purpose
public static class MyMagicHelper
{
    public static bool IsEmbeddedFile(string virtualPath)
    {
        // ... check if the path is an embedded file path
    }

    public static string GetPhysicalFilePath(string virtualPath)
    {
        // ... resolve the virtual file and return the correct physical file path
    }
}

As a last step you need to tell ASP.NET which host factory it should use. This is done in the web.config:

<system.web.webPages.razor>
    <host factoryType="My.Custom.Namespace.MyCustomRazorHostFactory" />
</system.web.webPages.razor>

I know my answer comes a bit late but hopefully someone else can make use of it when stumbling across this question as I did. :)

这篇关于不能调试通过自定义加载EmbeddedResource意见的VirtualPathProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 03:19