如何在web.config中定义using语句?

时间:2022-06-06 11:26:48

I'm using MySql in my asp.net project. But I don't want to type every "using MySql.Data.MySqlClient;" statement in every aspx.cs/aspx.vb file. How can I define this lines in web.config file?

我在我的asp.net项目中使用MySql。但是我不想每次都输入“使用MySql.Data.MySqlClient;”每个aspx.cs / aspx.vb文件中的语句。如何在web.config文件中定义这一行?

I've defined some namespaces like below but this only works for aspx pages:

我已经定义了一些名称空间,如下所示,但这仅适用于aspx页面:

<?xml version="1.0"?>
<configuration>
    <system.web>
        <compilation debug="false" targetFramework="4.0"/>
        <pages>
            <namespaces>
                <add namespace="System.Web.Configuration"/>
                <add namespace="MySql.Data"/>
                <add namespace="MySql.Data.MySqlClient"/>
            </namespaces>
        </pages>
    </system.web>
</configuration>

related question : Define common namespaces for code pages in Web.Config

相关问题:在Web.Config中为代码页定义通用名称空间

3 个解决方案

#1


6  

There is no way to setup global usings for the code-behinds. You have to put the usings in the code files.

没有办法为代码隐藏设置全局使用。您必须将使用放在代码文件中。

#2


4  

Yes you can. If you open %Program Files%\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip, Or: %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033

是的你可以。如果您打开%Program Files%\ Microsoft Visual Studio 8 \ Common7 \ IDE \ ItemTemplates \ CSharp \ 1033 \ Class.zip,或者:%Program Files%\ Microsoft Visual Studio 9.0 \ Common7 \ IDE \ ItemTemplates \ CSharp \ Code \ 1033

You can modify the class.cs file within that's used to generate all new C# source files - it looks like this:

您可以修改用于生成所有新C#源文件的class.cs文件 - 它看起来像这样:

using System;
using System.Collections.Generic;
using System.Text;

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

Also, there is a file called Class.vstemplate. Open this and you can edit the following:

此外,还有一个名为Class.vstemplate的文件。打开此,您可以编辑以下内容:

<Reference>
    <Assembly>System</Assembly>
        </Reference>
        <Reference>
            <Assembly>System.Data</Assembly>
        </Reference>
        <Reference>
            <Assembly>System.Xml</Assembly>
        </Reference>
    </References>

#3


1  

Just wrap your using block in a #region and collapse it. No more worry about how many usings there are.

只需将您的使用块包装在#region中并将其折叠即可。不再担心有多少使用。

#1


6  

There is no way to setup global usings for the code-behinds. You have to put the usings in the code files.

没有办法为代码隐藏设置全局使用。您必须将使用放在代码文件中。

#2


4  

Yes you can. If you open %Program Files%\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip, Or: %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033

是的你可以。如果您打开%Program Files%\ Microsoft Visual Studio 8 \ Common7 \ IDE \ ItemTemplates \ CSharp \ 1033 \ Class.zip,或者:%Program Files%\ Microsoft Visual Studio 9.0 \ Common7 \ IDE \ ItemTemplates \ CSharp \ Code \ 1033

You can modify the class.cs file within that's used to generate all new C# source files - it looks like this:

您可以修改用于生成所有新C#源文件的class.cs文件 - 它看起来像这样:

using System;
using System.Collections.Generic;
using System.Text;

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

Also, there is a file called Class.vstemplate. Open this and you can edit the following:

此外,还有一个名为Class.vstemplate的文件。打开此,您可以编辑以下内容:

<Reference>
    <Assembly>System</Assembly>
        </Reference>
        <Reference>
            <Assembly>System.Data</Assembly>
        </Reference>
        <Reference>
            <Assembly>System.Xml</Assembly>
        </Reference>
    </References>

#3


1  

Just wrap your using block in a #region and collapse it. No more worry about how many usings there are.

只需将您的使用块包装在#region中并将其折叠即可。不再担心有多少使用。