简体   繁体   中英

Mixing C# and VB.Net in ASP.Net App_Code folder

I have successfully access a class from VB to C# when I put the VB files to a folder and write this line of codes to web.config

 <compilation> <codeSubDirectories> <add directoryName="VB" /> <add directoryName="CSharp" /> </codeSubDirectories> </compilation> 

Now for some reason, I don't want to put the VB files to a sub directory and just put it to App_Code, like this.

However, I'm having an error when I'm trying to access the class from VB to C#. Here.

在此处输入图片说明

My question is, is it possible to access the class from VB to C# without putting the VB files to a sub directory? What should I add to web.config to make them connected? Thank you.

First of all, right-click every VB code file and navigate to "Properties" window to see properties of each file, then ensure all of them already have Build Action set to Compile mode such like this:

构建动作编译

If you're copied VB.NET code files from other location to App_Code directory or found using any different namespace(s), don't forget to set their current namespace properly:

Namespace Website.App_Code
    Module App
       ...
       ' App class contents here

       ...
    End Module
End Namespace

Then, you can use Website.App_Code.App class by referencing its namespace:

using Website.App_Code; 

public class Practice
{
    public static string AttachmentDirectory => App.Attachment;
}

Note that App class needs to be set as public scope so that it can be accessed by all instances across your project.

NB: The AttachmentDirectory property seems assigned with expression-bodied member syntax which applicable to version 6.0 or above. Check for currently used compiler version to use that kind of syntax.

Similar issues:

Classes residing in App_Code is not accessible

How to call a class in the App_Code folder from code behind?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM