automatic namespace importing

N

Natan

When you create and aspx page, this is generated by default:

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

Is there any way to automatically import more namespaces than the
default in aspx pages through some configuration in web.config?
I hate to keep all that <%@ Import Namespace="xxx" %> in all the pages...

I know in 2.0 there is a configuration tag, but I didn't find any in 1.1...
 
H

Hans Kesting

Natan said:
When you create and aspx page, this is generated by default:

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

Really? The list I get is different.
Is there any way to automatically import more namespaces than the
default in aspx pages through some configuration in web.config?
I hate to keep all that <%@ Import Namespace="xxx" %> in all the
pages...
I know in 2.0 there is a configuration tag, but I didn't find any in
1.1...

I don't think so: web.config controls how the application *works*,
and the "using" directive is just a hint to the compiler that it needs
to look into these namespaces as well when looking for some classname
that you used.

I found a template that looks as if it is used for the initial code-behind file.
Try changing that to "use" the namespaces you want!
On my system it was at:
C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\DesignerTemplates\1033\NewWebFormCode.cs

Hans Kesting
 
A

Anders Norås [MCAD]

Is there any way to automatically import more namespaces than the default
in aspx pages through some configuration in web.config?
No, not in web.config, but you can create your own base class (inheriting
from System.Web.UI.Page), import your namespaces in this class and make all
your pages inherit from this class instead of System.Web.UI.Page.
I hate to keep all that <%@ Import Namespace="xxx" %> in all the pages...
If you have a codebehind file with all the imports you do not need to add
Import directives to the ASPX page, these are allready imported by the code
behind class.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
N

Natan

Hans said:
Really? The list I get is different.

That is what is generated by default in .NET 1.1 (I use SP1, but i don`t
think it changes anything)
I don't think so: web.config controls how the application *works*,
and the "using" directive is just a hint to the compiler that it needs
to look into these namespaces as well when looking for some classname
that you used.

Not really. Actually there was this tag in .NET 1 beta... but for some
reason they removed. They are putting back in .NET 2.0 since it makes sense.

Web.config controls not only how your application works, but how your
pages are compiled, including linking other libraries, debugging and
other options.
I found a template that looks as if it is used for the initial code-behind file.
Try changing that to "use" the namespaces you want!
On my system it was at:
C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\DesignerTemplates\1033\NewWebFormCode.cs

Sorry, I think you are talking about code behind. I`m talking about aspx
pages. Different things, different places.
 
N

Natan

Anders said:
No, not in web.config, but you can create your own base class (inheriting
from System.Web.UI.Page), import your namespaces in this class and make all
your pages inherit from this class instead of System.Web.UI.Page.

That doesn`t solve the problem that i want to import namespaces in aspx
pages...
If you have a codebehind file with all the imports you do not need to add
Import directives to the ASPX page, these are allready imported by the code
behind class.

I don't think so... try yourself and your will get an compiller error.

"Code-behind" is just the name of a directive for VS.NET find the file
that should have the class the page extends. What really happens is
inheritance of classes, and that works exactly like any other
inheritance in .NET.

Code-behind and ASPX pages are not the same thing, they are separated.
Each one is one class and each one has its own using directives.
 
J

Juan T. Llibre [MVP]

re:
No, its about importing namespaces in aspx pages.
VS.NET has nothing to do with it.

Huh ?

Your question was :
re:
When you create and aspx page, this is generated by default:

Presumably, that was generated by VS,
so VS.NET *has* something to do with it, doesn't it ?

re:
using System;
using System.Collections;
etc.

Those are the imported namespaces in a .cs file

re:
through some configuration in web.config?

No. You can't do it via web.config but
you *can* do it by defining a new .aspx page
template, as explained in that article.

http://msdn.microsoft.com/library/d...ry/en-us/dv_vstechart/html/customwebwiz.aspTo import any number, or all classes, if you choose,all you have to do is create a "Custom Web Form"i.e., aspx page, Wizard, and add the controls/classesyou want to the template.Once you do that, the additional classes you specifywill be automatically included in your code-behind filefor all aspx pages which you create with the CustomWeb Form template.If I missed something, please let me know.Also, if you find a better way to do that, please post it.Juan T. Llibre==========="Natan" <[email protected]> wrote in messagenews:[email protected]...> Juan T. Llibre [MVP] wrote:>> I think what you are all looking for is>> building a custom Web Form Wizard.>>>> There's complete instructions at :>>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/customwebwiz.asp>>>> Once you do that, your custom template will>> be available via the "Add a New Item" menu.>> No, its about importing namespaces in aspx pages.> VS.NET has nothing to do with it.
 
J

Juan T. Llibre [MVP]

Wow, the text formatting was really mangled.
Here's a properly formatted ( I hope ) post:

re:
No, its about importing namespaces in aspx pages.
VS.NET has nothing to do with it.

Huh ?

Your question was :
re:
When you create and aspx page, this is generated by default:

Presumably, that was generated by VS,
so VS.NET *has* something to do with it, doesn't it ?

re:
using System;
using System.Collections;
etc.

Those are the imported namespaces in a .cs file

re:
through some configuration in web.config?

No. You can't do it via web.config but
you *can* do it by defining a new .aspx page
template, as explained in that article :

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/customwebwiz.asp

To import any number, or all classes, if you choose,
all you have to do is create a "Custom Web Form",
i.e., aspx page, Wizard, and add the controls/classes
you want to the template.

Once you do that, the additional classes you specify
will be automatically included in your code-behind file
for all aspx pages which you create with the Custom
Web Form template.

If I missed something, please let me know.
Also, if you find a better way to do that, please post it.



Juan T. Llibre
==========
 
N

Natan

Juan said:

Sorry, but probably you have no idea about what i'm talking about.

Your question was :


Presumably, that was generated by VS,
so VS.NET *has* something to do with it, doesn't it ?

No. Creating an aspx page is "create an empty file with .aspx extension
using notepad and run it". There is no need for VS.NET... And actually
even though I have VS.NET installed I think it is much more productive
to not use it for ASP.NET pages.

So, if you had no idea that you could do it:

start menu -> run -> notepad
paste this:

<%@ Page Language="C#"%>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
bla
}
</script>

Save as .ASPX and run. There will be an error because of the "bla" and
there is a link tho show the "Complete Compilation Source". That is the
ASPX page that is actually compiled.

If you specify an Inherits="xxxx" in the Page directive, what happens is
that this class extends yous class. "Code behind" directive is only a
way to tell vs.net what file is associated so it can create the treeview
or open it. It is just a base class for inheritance, nothing more. You
can do the same things without it.
re:


etc.

Those are the imported namespaces in a .cs file

No, those are namespaces imported by default when ASP.NET parses your
..ASPX page and automatically generates a class in the ASP namespace to
compile. See above...

No. You can't do it via web.config but
you *can* do it by defining a new .aspx page
template, as explained in that article.

Yes, that's what thought. There was this functionality in .net 1 beta
but was removed for some reason. It's coming back in .NET 2.0 (thank god)

* http://msdn2.microsoft.com/library/bd818k6y.aspx

But the solution has nothing to do with templates or VS.NET. The only
way I see to solve this is with <%@ Import Namespace="xxx" %> in every page.
 
J

Juan T. Llibre [MVP]

re:
Yes, that's what thought. There was this functionality in .net 1 beta but
was removed for some reason. It's coming back in .NET 2.0 (thank god)

* http://msdn2.microsoft.com/library/bd818k6y.aspx

See
http://msdn.microsoft.com/library/d.../en-us/dv_vstechart/html/customwebwiz.aspThat article says that VS 2002 and VS 2003 can do that.( VS 2002 = .NET 1.0 , and VS 2003 = NET 1.1 )re:<imports> <add namespace="MyNamespace"/></imports>You must realize that loading a whole bunch of Namespacesat the Application level involves a serious waste of resources.It's *far* better to create a custom Web Form templateand only load the Namespaces at the Page level.re:> No. Creating an aspx page is "create an empty file with .aspx extensionusing notepad and run it". There is no need for VS.NET.If you're intent on using Notepad, and have VS,you must be some sort of a masochist ... :)For that, you might as well have a pre-written .txt filewhich you open and save as .aspx, with all the stuffyou want inserted.When you defined your question as :"When you create and aspx page, this is generated by default"the obvious conclusion is that you're using VS.NET,since Notepad doesn't "generate" anything.re:> Sorry, but probably you have no idea about what i'm talking about.If you change your mind between posts it'shard to know what you're talking about. :)Juan T. Llibre==========="Natan" <[email protected]> wrote in messagenews:[email protected]...> Juan T. Llibre [MVP] wrote:>> Huh ?>> Sorry, but probably you have no idea about what i'm talking about.>>>> Your question was :>>>>>When you create and aspx page, this is generated by default:>>>> Presumably, that was generated by VS,>> so VS.NET *has* something to do with it, doesn't it ?>> No. Creating an aspx page is "create an empty file with .aspx extensionusing notepad and run it". There is no need for VS.NET... And actually eventhough I have VS.NET installed I think it is much more productive to not useit for ASP.NET pages.>> So, if you had no idea that you could do it:>> start menu -> run -> notepad> paste this:>> <%@ Page Language="C#"%>> <script runat="server">> void Page_Load(object sender, EventArgs e)> {> bla> }> </script>>> Save as .ASPX and run. There will be an error because of the "bla" andthere is a link tho show the "Complete Compilation Source". That is the ASPXpage that is actually compiled.>> If you specify an Inherits="xxxx" in the Page directive, what happens isthat this class extends yous class. "Code behind" directive is only a way totell vs.net what file is associated so it can create the treeview or openit. It is just a base class for inheritance, nothing more. You can do thesame things without it.>>> re:>>>>>using System;>>>using System.Collections;>>>> etc.>>>> Those are the imported namespaces in a .cs file>> No, those are namespaces imported by default when ASP.NET parses your.ASPX page and automatically generates a class in the ASP namespace tocompile. See above...>>>> No. You can't do it via web.config but>> you *can* do it by defining a new .aspx page>> template, as explained in that article.>> Yes, that's what thought. There was this functionality in .net 1 beta butwas removed for some reason. It's coming back in .NET 2.0 (thank god)>> * http://msdn2.microsoft.com/library/bd818k6y.aspx>> But the solution has nothing to do with templates or VS.NET. The only wayI see to solve this is with <%@ Import Namespace="xxx" %> in every page.
 
N

Natan

Juan said:
..NET 1.0 , and VS 2003 = NET 1.1 )

The article teaches how to extend visual studio creating templates for
webforms and code behind. Nothing to do with what i asked.

Visual Studio CAN'T do that because it's not up to VS how ASP.NET pages
are parsed and compiled.

I suggest you take a look at .NET documentation to understand how
asp.net pages work so you will understand what i'm talking about.
 
J

Juan T. Llibre [MVP]

I wonder why this reformatting is happening.

If I send in text format, the format is lost.
But, it's preserved if I send HTML.

Here's a reformatted reply:

See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/customwebwiz.asp

That article says that VS 2002 and VS 2003 can do that.
( VS 2002 = .NET 1.0 , and VS 2003 = NET 1.1 )


re:
<imports>
<add namespace="MyNamespace"/>
</imports>

You must realize that loading a whole bunch of Namespaces
at the Application level involves a serious waste of resources.

It's *far* better to create a custom Web Form template
and only load the Namespaces at the Page level.

re:
No. Creating an aspx page is "create an empty file
with .aspx extension using notepad and run it".
There is no need for VS.NET.

If you're intent on using Notepad, and have VS,
you must be some sort of a masochist ... :)

For that, you might as well have a pre-written .txt file
which you open and save as .aspx, with all the stuff
you want inserted.

When you defined your question as :
"When you create and aspx page, this is generated by default"
the obvious conclusion is that you're using VS.NET,
since Notepad doesn't "generate" anything.

re:
Sorry, but probably you have no idea
about what i'm talking about.

If you change your mind between posts it's
hard to know what you're talking about. :)




Juan T. Llibre
===========
 
J

Juan T. Llibre [MVP]

re:
Nothing to do with what i asked.

<sigh>

Would you please re-explain what it is you asked ?

( Particularly, how Notepad would "generate" what you want inserted ? )




Juan T. Llibre
===========
 
N

Natan

Juan said:
re:

<sigh>

Would you please re-explain what it is you asked ?

( Particularly, how Notepad would "generate" what you want inserted ? )

Notepad doesn't generate anything, asp.net does.

Sorry, i respect you, but i won't explain again since you don't know
what i'm talking about. What i asked is written in the first post...
 
T

TJS

I don't believe there is such a thing.

I think this is one of the big pitfalls with .net, there is no way of
knowing which namespaces to import. I always have to test and get an error
message to find out what is needed. usually end up putting in too many
 
N

Natan

TJS said:
I don't believe there is such a thing.

Yeah... I have seen lots of people asking about this... .NET 2.0 will
solve this. until this day, lots of @import to deal... hope they don't
remove it again when the final comes out.
I think this is one of the big pitfalls with .net, there is no way of
knowing which namespaces to import. I always have to test and get an error
message to find out what is needed. usually end up putting in too many

I don't think there is any problem having tons of "using", since it is
used by the compiler to find the types. Once it compiles to MSIL, every
type is the full qualified path, so it's not like it is searching all
the namespaces again everytime it runs. So there is really no difference
having 1 or 100 usings... But I may be wrong.

Thanks for understanding me :p
 
J

Juan T. Llibre [MVP]

re:
Sorry, i respect you, but i won't explain again since you don't know
what i'm talking about. What i asked is written in the first post.

Nathan,

Your original question was :
Is there any way to automatically import more namespaces than
the default in aspx pages through some configuration in web.config?
I hate to keep all that <%@ Import Namespace="xxx" %> in all the pages...

The answer is *no*, you cannot do that via web.config,
at least not until .Net 2.0 is your Framework version.

Your original premise:
When you create and aspx page, this is generated by default:

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

is flawed, as you don't necessarily have to import
all those namespaces, and Notepad doesn't
"generate" anything like that.

In fact, Notepad doesn't generate anything.

In order to "generate" anything,
you must have been using VS.NET.

When I create a new C# Web Project,
the following classes are imported by default:


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

That's a far cry from what you're
saying is imported by default.

Like I said, if you need more, or even less, classes
imported by default, in ASP.Net 1.0 or 1.1, if you're
using VS.NET 2002 or VS.NET 2003, the best
way to do it is to create a custom template for aspx pages.

Clearly, with Notepad you won't be able to do that,
but Notepad is your very particular pre-requisite.

For anybody else, using VS.NET 2002 or 2003,
creating a custom aspx template will do the job
neatly, for ASP.NET 1.0 or 1.1.

For anybody using VS.NET 2005 ( .Net Framework 2.0 )
one way to do it is to use the <imports> statement in
web.config, although doing that will generate excessive
memory usage.

The best way to do that in VS.NET 2005 is, still, to create
a Custom Web Form template which you can use for
individual pages, but not load the classes for all pages,
since that would, again, cause excessive memory consumption.

If you want to acknowledge that, fine.
If you don't want to acknowledge that, it's fine, too.

Let's move on, please.



Juan T. Llibre
===========
 
N

Natan

Juan said:
is flawed, as you don't necessarily have to import
all those namespaces, and Notepad doesn't
"generate" anything like that.

Those are generated automatically by ASP.NET when it parses the page and
creates the class.
In fact, Notepad doesn't generate anything.

In order to "generate" anything,
you must have been using VS.NET.

Of course notepad doesn't generate anything. (duh) You are getting it
wrong...

The fact is that you didn't understand how aspx pages works. Since the
page itself is a class, the page is converted to a class extending
System.Web.UI.Page and compiled. Not the code behind, but the page. All
the HTML you create is parsed and there is a lot of LiteralControls to
hold all the html you write.

When you use code behind, what happens actually is that this class
ASP.NET creates automatically from the aspx file inherits from the class
you (or vs.net) specify instead of System.Web.UI.Page.

And this code generated by asp.net must be a class, and by default have
all those using clauses.

Do the test, copy the code i posted before, paste in notepad, save as
aspx file and run it in your wwwroot. You will see what i'm talking
about...

When I create a new C# Web Project,
the following classes are imported by default:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

That's a far cry from what you're
saying is imported by default.

Yes. That's what you get when you use CodeBehind... but since i told you
i'm not talking about code behind neither VS.NET...
If you want to acknowledge that, fine.
If you don't want to acknowledge that, it's fine, too.

Let's move on, please.

The problem here is that i'm talking about A and you're talking about B.
You didn't understand what i'm talking about and want me to accept your
answer. Please, read again the posts and try to understand. You are the
one who must acknowledge things.

Let's move on... =)
 
J

Juan T. Llibre [MVP]

Natan,

Could you let me know how you determined that
Those are generated automatically by ASP.NET
when it parses the page and creates the class.
re:
Since the page itself is a class, the page is converted
to a class extending System.Web.UI.Page and compiled.
Not the code behind, but the page. All the HTML you create
is parsed and there is a lot of LiteralControls to hold all the html you write.

Where did you find the list of Imports statements you quoted ?

i.e., I compiled the test you posted, to check it out.

---------------
<%@ Page Language="C#"%>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
bla;
}
</script>
----------------------

( I added the ";", so it would compile )

You requested that I do this :
------------------------------------------
"Save as .ASPX and run. There will be an error because of the "bla" and
there is a link tho show the "Complete Compilation Source". That is the
ASPX page that is actually compiled.
---------------------------------

I did that and got this list of compiled page resources
in the "Complete Compilation Source" :

System.EnterpriseServices.dll
acme.dll
global.asaxxi3ltqav.dll
System.Web.Services.dll
System.dll
System.Drawing.dll
System.Web.dll
System.Data.dll
mscorlib.dll
System.Web.Mobile.dll
System.Xml.dll
i8fuexe.dll
i8fuexe.0.cs
i8fuexe.1.cs

Then, you said that:
----------------------------
If you specify an Inherits="xxxx" in the Page directive,
what happens is that this class extends yous class.
---------------------------

So, I added
<%@ Import Namespace ="System.Diagnostics" %>
to the page, since you said that would add a namespace
which should have been compiled, if you're right, given that
you said that :
-----------------------------------
"those are namespaces imported by default when ASP.NET
parses your .ASPX page and automatically generates a class
in the ASP namespace to compile.
But the solution has nothing to do with templates or VS.NET.
The only way I see to solve this is with
<%@ Import Namespace="xxx" %> in every page."
--------------------------------------

I, then, added
<%@ Import Namespace ="System.Diagnostics" %>
to satisfy your requisite, as you said to do.

But, the new list of compiled page resources only has :

System.EnterpriseServices.dll
acme.dll
global.asaxxi3ltqav.dll
System.Web.Services.dll
System.dll
System.Drawing.dll
System.Web.dll
System.Data.dll
mscorlib.dll
System.Web.Mobile.dll
System.Xml.dll
7ion-da0.dll
7ion-da0.0.cs
7ion-da0.1.cs

and does *not* have a reference to "System.Diagnostics.dll",
as it should have been added if I understood what you say,
and what you explain is correct, that the "Imports" Page
statement would add the "System.Diagnostics" to the list
of compiled resources.

If I did *not* understand what you said, please detail the method(s)
you used to determine which dll's are included in the compiled aspx page,
and how/when/where another namespace, added via <%@ Import Namespace ="Bla.Bla" %>
to the page, is added to the compiled list of resources for that page.

This will make your position a bit easier to understand.
Please take the time to document this, as I have done.

Don't take offense. Just prove your point.

This is extremely interesting, and I'd like to know
if you are actually right, because it would certainly
change the way I've understood, up until now,
the .NET Framework and ASP.NET's inner workings.




Juan T. Llibre
===========
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top