开发者

apply common namespace from web.config to app_code class file

I am relatively new to asp.net environment.

Recently working on a project, i discovered a problem

i.e. I had to add using system.io to all file functions in any page. same for using sql functions i.e. any method for that namespace had to be used in full like

System.IO.FileStream fs = null;

in place of

FileStream fs = null;

So in short it not only needs more typing time, it is also hard to remember the namespace behind certain methods.

However i found a solution.

By adding the following section in web.config , ALL my pages automatically have access to that namespace

<namespaces>

        <add namespace="System.Data"/>
        <add namespace="System.Data.SqlClient"/>
        <add namespace= "MySql.Data.MySqlClient"/>
        <add namespace="System.IO"/>
</namespaces>

Having sorted out the above problem. I am now facing a new one. My application has a number of code pages in app_code folder. In app_code folder , all of my pages require the following header

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;

One might be puzzled, as to why the declarations placed in web.config are not working for the classes placed in this folder. They are part of the same web application. Now i have to take care of that as well in all documents.

I would be thankful, if one could share th开发者_开发技巧e information regarding how to have common namespaces in app_code folder as well.


You can not, according to the documentation the using directive is per file:

The scope of a using directive is limited to the file in which it appears.

But there is a shortcut to add required using directive to a file.
If you type something that is not in the current using's you get a blue underscore under the first letter move the mouse there and click to get a popup menu (or press CTRL+.), in the menu, select "using ..." and Visual Studio automatically inserts the correct using statement at the top of your file.


I believe this is due to the special nature of App_Code. Because the files in App_Code are typically class files (i.e. not webforms, user controls, etc, just code files), they are all compiled with just the specific language compilers (csc.exe or vbc.exe). The other pages in the project (this time all of the webforms, etc...) are compiled with the ASP.NET compiler, which takes into account the contents of the web.config file.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜