开发者

Is it possible to run VB .NET and C# in different pages on a .NET web app?

开发者 https://www.devze.com 2023-01-31 06:40 出处:网络
Is it possible to run VB .NET an开发者_Python百科d C# in different pages on a .NET web app? Picking up a legacy VB .NET project but need to add pages to it, is it possible to write these in C# or ca

Is it possible to run VB .NET an开发者_Python百科d C# in different pages on a .NET web app?

Picking up a legacy VB .NET project but need to add pages to it, is it possible to write these in C# or can you only have one language per project?


Reply, actually it is possible to run both VB and C# in an ASP.NET project. Please see this post on Tim Heuer's blog. http://timheuer.com/blog/archive/2007/02/28/14002.aspx


Add the following lines to the web.config

<configuration>
<system.web>
    <compilation>
        <codeSubDirectories>
            <add directoryName="VB_Code"/>
            <add directoryName="CS_Code"/>
        </codeSubDirectories>
    </compilation>
</system.web>

Create a sub-folder in the App_Code folder for each language you want to support. For Example:

/App_Code/VB_Code

/App_Code/CS_Code

Place your VB.NET code in the VB_Code folder and place C# code in the CS_Code folder


If this is an ASP.NET project then yes, you can, but only for ASP.NET elements like pages and master pages. You would only have to change the Language attribute on the main directive (like <%@Page) to C# (or add one if it does not exist). This works because each page is compiled into a separate assembly by the ASP.NET service.

You will not be able to switch languages like this for code-behind files though, since those are all pre-compiled into the same assembly.

However, if I were working on this project with you, I'd be inclined to LART you for doing something like this...


You can use more than one, but it's not trivial, and the hassle required isn't probably worth the gain. After all, pretty much everything you can do in C# can also be done in VB, only the syntax is uglier.

Mixing C# and VB on a per-assembly level, within the same solution, and referencing each other, is really easy though, so if you need to add an entirely new module, you can write it in C# and reference it from the existing VB application.

0

精彩评论

暂无评论...
验证码 换一张
取 消