【发布时间】:2017-10-20 19:05:12
【问题描述】:
Fresh VS2017 网站项目有一个这样定义的 Default.aspx。
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
我已经为它提供了一个像这样的 Default.aspx.vb 文件:
Partial Public Class _Default : Inherits Page
Public Function Teapot() As Boolean
Return True
End Function
End Class
还有一个像这样的 Default.aspx.cs 文件:
public partial class _Default : System.Web.UI.Page
{
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e); // There is a breakpoint on this line
Teapot(); // Error: The name 'Teapot' does not exist
in the current context
}
}
尽管在使用 Teapot() 时出现设计时错误,但网站确实运行了,并且断点被标记为 The breakpoint will not currently be hit. 就好像 C# 文件的内容没有包含在项目中,尽管使用部分类并在 web.config 中包含以下内容:
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers></system.codedom>
如何获取包含在 _Default 页面类中的 C# 和 VB 文件的内容?
【问题讨论】:
-
您不能将 C# 和 VB 部分类组合在一起 - 它们使用不同的编译器。
-
我知道,但是由于它们都编译为 MSIL,我认为也许他们可以在那个阶段加入。
-
好的,使用Ujjwal在接受的答案中指出的文章中描述的App_Code的两个子文件夹,您可以用一种语言编写一个可以从_Default类中调用的类其他语言(都在同一个 Web 项目中)。与跨两种语言的部分类不同,但非常接近。