2

The 'ASP' namespace seems to be a generated because it's not used in the website project's source code, anywhere (or at least not explicitly used by the programmer of the website). I would like to know how it is fully realized.

For example:
Is it the responsibility of MSBuild to drive creation of the ASP namespace and if so, where is that instruction found? I know the C# compiler won't create a namespace from nothing of its own volition, so the ASP namespace must be fed into it, even if not used by the website programmer. Maybe it's generated into the source code by a different tool. The 'Temporary ASP.NET Files' folder might have some bearing on it. As you can see, I want all the gory details in order to unlock and understand that namespace ...

Visual Studio seems to have tooling that allows the ASP namespace to be used (IntelliSense support for it) but that masks my understanding of it.

How is the 'ASP' namespace realized in a website from start to finish?

(I haven't found a good article that explains all this.)


Here the ASP namespace is shown in .NET Reflector. (This image taken from Rick Strahl's blog)

enter image description here

John K
  • 28,441
  • 31
  • 139
  • 229
  • What do you mean by the "ASP Namespace"? Are you referring to the prefix used for web controls in the source view of the HTML designer? – Rick Liddle Aug 10 '11 at 22:36
  • @Rick - Yes, I believe that it's the same one seen in the compiled IL, like shown in this screenshot from .NET Reflector http://www.west-wind.com/weblog/images/11/o_PageInheritance.png – John K Aug 10 '11 at 22:38

3 Answers3

1

The ASP. namespace can be used to Load dynamically a custom control with more safe that the cast will works.

You can control what name the custom control can take in the ASP. namespace by placing the ClassName="ControlClass" on the declaration of the name space and the dynamic control now will have a reference to ASP.ControlClass to make a secure cast when you use the LoadControl

You can read the full steps on MSDN http://msdn.microsoft.com/en-us/library/c0az2h86(v=vs.100).aspx

When you do not use the ASP. namespace and left the control takes an automatic name, then the case may fail (I do not know why, but its fail on my sever time to time) The reference that there created are

namespace ASP
{
    [CompilerGlobalScope]
    public class Control_Class_nameByDirectory : ControlClass
    {
        [DebuggerNonUserCode]
        public ControlClass();

        protected override bool SupportAutoEvents { get; }

        [DebuggerNonUserCode]
        protected override void FrameworkInitialize();
    }
}

And when you try to make a cast like (ControlClass)LoadControl("~/module/Control.ascs") it may fail because is recognize it as Control_Class_nameByDirectory and not as ControlClass

Now if you declare the ClassName on the control header as MSD says, the result is the control to get the same ClassName as the one you have define :

namespace ASP
{
    [CompilerGlobalScope]
    public class ControlClass : global::ControlClass
    {
        [DebuggerNonUserCode]
        public ControlClass();

        protected override bool SupportAutoEvents { get; }

        [DebuggerNonUserCode]
        protected override void FrameworkInitialize();
    }
}

and here you can use the ASP.ControlClass to cast the control with out worry if its fail.

So following the steps that described here http://msdn.microsoft.com/en-us/library/c0az2h86(v=vs.100).aspx you can avoid issues like that. (and that I have face them)

The issue of failing to case a custom control with out reference to ASP. namespace have been seen both on Dot net 4.0 and 4.5 versions. And the worst is that is a random failure - meaning that some times is happens, some time not, and I was unable to find the reason.

Aristos
  • 66,005
  • 16
  • 114
  • 150
0

I'm going to assume that you're looking for the prefix on server control tags... if that's not the case, let me know.

The prefix is registered in the web.config file under configuration/system.Web/pages/controls. You can modify it if you want, or register additional prefixes for your own control libraries.

MSDN info here: http://msdn.microsoft.com/en-us/library/ms164640.aspx

Rick Liddle
  • 2,684
  • 19
  • 31
  • I don't see a bridge though, in this initial answer - if that is the source of the ASP namespace, then how does it make it into a compiled DLL, or does it? – John K Aug 10 '11 at 22:55
  • @John - No, that's not what I thought you were talking about. I haven't seen that before, but I'll admit I haven't been poking around web projects with Reflector lately. :-) I'll look and see if I see the same thing in other web apps. – Rick Liddle Aug 10 '11 at 22:58
0

This namespace is used on the code that ASP.NET generates from parsing your .aspx and .ascx files.

I have no idea why you care, though. It's just a namespace. There's nothing "special" about it, and you shouldn't be referencing anything in that namespace.


To understand how all of this works, read "ASP.NET Life Cycle".

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • John, it stems from a problem with a Continuous Integration system. However in general I think it's a valid question even from an academic point of view, so I do care. You're right though, as a developer, there's likely no need to care. I would like for other people to understand too when they visit this question even if it's out of curiosity. – John K Aug 10 '11 at 23:28
  • Do you know how it gets from the parsed information in the aspx and ascx files and through to the final compiled DLL? I'm hoping for an explanation of stages and things it goes through. – John K Aug 10 '11 at 23:33
  • Yes, I do. It's called the [ASP.NET Life Cycle](http://msdn.microsoft.com/en-us/library/ms227435.aspx). And please state _why_ your CI system needs to know this. I do CI of web applications using TFS, and have no idea why any code would care about it. In particular, I don't know that Microsoft has guaranteed how they generate their code, so ask yourself what would happen if the name of the namespace changed daily, just to mess with you? – John Saunders Aug 10 '11 at 23:41
  • BTW, if you're asking because you're trying to set up CI to work with an ASP.NET Web Site (File->New Web Site), then my answer is, "this is why not to use ASP.NET Web Sites for serious work". Instead, use File->New Project to create a Web Application Project (going forward). – John Saunders Aug 10 '11 at 23:49
  • John, I don't want to mix the CI question with this one. I was told earlier today the a developer posted the CI question to support and he may have put a distinct question on SO. The CI issue was the catalyst that made me realize I didn't understand this namespace. I think it would be worthwhile to have a solid answer devoted to it, for whoever else has questions it, and I couldn't think of a better place to seek that out than StackOverflow. After searching the Internet I came across similar questions but nothing solid. Eventually I'll assign a bounty to it but am confident SO will prevail – John K Aug 10 '11 at 23:51
  • Agreed about not using websites for serious work, but sometimes business-driven software decisions come into play, and dictate you have to program in a CMS system implemented as a website project. :/ Would love to not to work in website projects anymore. But I digress. – John K Aug 10 '11 at 23:54
  • If you find that "CI question", then please link it here. I'd be fascinated to learn how any piece of code cares about the namespace, especially given that the assembly names are random... – John Saunders Aug 11 '11 at 00:17
  • @JohnSaunders let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2342/discussion-between-john-k-and-john-saunders) – John K Aug 11 '11 at 00:27
  • Can dump it into the chat room ..... http://chat.stackoverflow.com/rooms/2342/discussion-between-john-k-and-john-saunders – John K Aug 11 '11 at 00:29