【问题标题】:Two codebehind files for one aspx, one VB and one C#一个 aspx、一个 VB 和一个 C# 的两个代码隐藏文件
【发布时间】: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=\&quot;Web\&quot; /optionInfer+"/>
</compilers></system.codedom>

如何获取包含在 _Default 页面类中的 C# 和 VB 文件的内容?

【问题讨论】:

  • 您不能将 C# 和 VB 部分类组合在一起 - 它们使用不同的编译器。
  • 我知道,但是由于它们都编译为 MSIL,我认为也许他们可以在那个阶段加入。
  • 好的,使用Ujjwal在接受的答案中指出的文章中描述的App_Code的两个子文件夹,您可以用一种语言编写一个可以从_Default类中调用的类其他语言(都在同一个 Web 项目中)。与跨两种语言的部分类不同,但非常接近。

标签: c# asp.net .net vb.net


【解决方案1】:

您不能在同一个项目中混合使用 vb 和 c# - 如果您在 Visual Studio 中注意到项目文件是 .vbproj 或 .csproj。您可以在解决方案中 - 在 vb 中有 1 个项目,在 c# 中有 1 个。

看起来你可以在 App_Code 目录的 web 项目中同时使用它们:

http://pietschsoft.com/post/2006/03/30/ASPNET-20-Use-VBNET-and-C-within-the-App_Code-folder.aspx

但是您可以使用 UserControl 来显示您的数据。如果您默认使用 CS,您可以从 .VB 代码在后端获取数据并将它们显示到用户控件中。否则,您可以使用 Ajax 获取数据并使用 HTML 显示这些数据和事件。 我希望通过这种方式,您可以显示 .vb 和 .cs 文件中的所有数据和事件。

【讨论】:

  • 好的,使用 App_Code 的两个子文件夹,您仍然不能在语言之间拆分部分类,但是您可以用一种语言定义一个辅助类,该类可以从编写的 _Default 类中调用其他语言。
  • "您不能在同一个项目中混合使用 vb 和 c# - 如果您在 Visual Studio 中注意到项目文件是 .vbproj 或 .csproj。" - 此规则不适用于“Visual Studio 网站”(尤其是那些使用旧 ASP.NET 2.0 (2005-2008) 网站模型的网站模型,其 App_Code 目录在运行时编译,而不是设计时编译,确实意味着您可以在同一个项目中混合使用 C# 和 VB.NET。
【解决方案2】:

我不知道是否有人还在阅读这篇文章,但您可以拥有一个使用 VB.NET 内联代码和 C# 代码隐藏的文件。您的文件定义如下所示

<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

您可以将 VB 代码放在同一页面内 &lt;script language = "VB" runat = "server"&gt;&lt;/script&gt;&lt;% %&gt; 分隔符内的块中,而不是将其放在代码隐藏中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多