How to define using statements in web.config?
Asked Answered
D

3

6

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?

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

<?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

Deconsecrate answered 16/4, 2010 at 11:33 Comment(0)
R
6

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

Rabbet answered 16/4, 2010 at 11:35 Comment(5)
+1. using is a statement in source code that is proccessed by C# compiler. web.config is for web server configurationTjaden
And aspx pages are compiled by...? This is not logical to me :)Deconsecrate
.aspx page - is just a markup with server-side code - they are also processed by web server. .aspx.cs are source files. You can name it even 1.cs - just don't forget to specify it into page declaration.Tjaden
P.S. Please use symbol @ before nick, i.e. @abatishchev, sou it would be easy to find your comment on my one. Thanks! :)Tjaden
@Tjaden OK. I googled all day but no solution. Probably there is no solution. Maybe this is why every code-behind page contains using System; :(Deconsecrate
L
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

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

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:

<Reference>
    <Assembly>System</Assembly>
        </Reference>
        <Reference>
            <Assembly>System.Data</Assembly>
        </Reference>
        <Reference>
            <Assembly>System.Xml</Assembly>
        </Reference>
    </References>
Lupien answered 16/4, 2010 at 11:44 Comment(3)
Sorry I can't explain myself clear. Simply I don't want to see that code there :). Also this solution would not be good because in some projects I use MySQL in some SQL Serve or Access...Deconsecrate
In that case, I think you had better mark @Tom's answer as correct, because it can't be done.Lupien
If you worry about managing "usings", I would suggest purchasing the Resharper add-in. It has an excellent template mechanism you can use to generate "usings" for different purposes if that's the way you want to go. It has a cleanup feature that removes unused "usings" and sorts them into logical order so it keeps the code tidy. It also has a feature that detects missing "usings" as you type code and prompts you to see if you want it to add the "using" for you. It does a whole lot more too. Highly recommended.Rabbet
P
1

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

Prepositor answered 16/4, 2010 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.