Results 1 to 22 of 22

Thread: [RESOLVED] How to make my own custom "page" object?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Resolved [RESOLVED] How to make my own custom "page" object?

    I want to create my own object (dll) that inherits from System.Web.UI.Page so that I can do some custom pre/post processing of a call to a web page that inherits from my class. So for example, lets say you have the following page defined:

    [code]
    Partial Class TestPage1
    Inherits MyCustomDLL.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Public Function doSomething(param1 as string, param2 as string) as String

    End Function

    End Class
    [code]

    So, when someone view this page in a web browser, my DLL should be able to grab the Request object, do something with it, and then pass control to the page. The page will do what it needs to do, and then pass control back to my DLL to issue the final Response object.

    Is this possible? Any pointers would be appreciated.

    Visual Studio 2010

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How to make my own custom "page" object?

    Hey,

    I think you are perhaps mixing terminology here.

    You would create a Base Page Class, which your Web Form would inherit from. This class could certainly be contained within your own assembly though.

    Here is an example:

    http://www.4guysfromrolla.com/articles/041305-1.aspx

    Hope that helps!

    Gary

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to make my own custom "page" object?

    Not the way you describe it. You might be thinking of an HttpHandler or HttpModule, but a base class doesn't 'call' the child page in the way you're describing. What is the reason you are trying to handle the request, pass the request, then handle it again?

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to make my own custom "page" object?

    I'm trying to make my own webservice type process. Where the user defines functions in a class, and because it inherits my class, I can handle the output to the http stream. So, I think the better way is using Attributes. So the user can define something like:

    <CustomAttribute()> _
    Public Function doSomething() as String
    Return "something"
    End Function

    Then, in my base class, somehow I can iterate over all functions the user has decorated with the <CustomAttribute> attribute and take the results, and spit it out via Response.Write or some http writer.

    So the user's class, actually doesn't know anything about ASP.NET, it simply inherits from my base class, which handles communication via the HTTP context.

    Now I just have to figure out how to do this.

    Visual Studio 2010

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to make my own custom "page" object?

    I've seen and worked with frameworks like this before. Using a base class or a class inheriting from Page isn't really relevant here because by doing what you're doing, you're taking over the entire rendering of the page - therefore the page and its properties aren't being used and therefore aren't required. You can do this in a generic handler, a regular aspx (without markup) or an HttpHandler which invokes and reads the attributes on the methods. Since the return value is a string, all you're doing is an HttpContext.Response.Write. So the user's class can be a normal class with a bunch of methods in it that have the [CustomAttribute()] on it.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to make my own custom "page" object?

    Quote Originally Posted by mendhak View Post
    I've seen and worked with frameworks like this before. Using a base class or a class inheriting from Page isn't really relevant here because by doing what you're doing, you're taking over the entire rendering of the page - therefore the page and its properties aren't being used and therefore aren't required. You can do this in a generic handler, a regular aspx (without markup) or an HttpHandler which invokes and reads the attributes on the methods. Since the return value is a string, all you're doing is an HttpContext.Response.Write. So the user's class can be a normal class with a bunch of methods in it that have the [CustomAttribute()] on it.
    Yes, that's exactly what I'm talking about! But I'm still a little unclear here. Let me know if this is correct:

    1) I create a base class that defines one or more custom attributes. This base class handles all requests/responses from/to the web browser.

    2) The end user creates a regular class and inherits from my class.

    3) The end user decorates any methods he wants exposed to my base class with the appropriate attributes

    4) When a request comes through, I parse the query string of the URL, to determine what the client is requesting.

    5) If I determine it is a function the client wants to call, I iterate through the methods that the end user has decorated.

    6) I then get the return value from the function, and write it out via the http context.

    Is that basically the idea?

    Visual Studio 2010

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to make my own custom "page" object?

    Okay, so I was able to do the following:

    Code:
    Imports Microsoft.VisualBasic
    Imports System.Reflection
    
    Public Class CustomPageClass
        Inherits System.Web.UI.Page
    
        <AttributeUsage(AttributeTargets.Method, Inherited:=True, AllowMultiple:=True)> _
        Public Class CustomAttribute
            Inherits System.Attribute
    
        End Class
    
        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim methods As MethodInfo() = Me.GetType.GetMethods
            For Each method As MethodInfo In methods
                Dim att As WebMethodAttribute = Attribute.GetCustomAttribute(method, GetType(CustomAttribute))
                If att IsNot Nothing Then
                    Response.Write(method.Name + "<br>")
                End If
            Next
        End Sub
    
    End Class
    Which gets all methods from derived class. I do see the method in there that I decorated with the custom attribute, but I'm wondering how to limit the results by GetMethods to just those methods that are decorated with a certain attribute?
    Last edited by softwareguy74; Nov 25th, 2009 at 05:09 PM.

    Visual Studio 2010

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to make my own custom "page" object?

    [quote]
    Yes, that's exactly what I'm talking about! But I'm still a little unclear here. Let me know if this is correct:

    1) I create a base class that defines one or more custom attributes. This base class handles all requests/responses from/to the web browser.

    Yes

    [qutoe]
    2) The end user creates a regular class XXand inherits from my classXX.
    My point is that they don't have to inherit from anything, the inheritance is a red herring/moot point. It won't make a difference. BBCode doesn't allow for a strikethrough. I tried [s]strike[/s] and [strike]strike[/strike] but can't do it.

    3) The end user decorates any methods he wants exposed to my base class with the appropriate attributes

    4) When a request comes through, I parse the query string of the URL, to determine what the client is requesting.

    5) If I determine it is a function the client wants to call, I iterate through the methods that the end user has decorated.
    5) If I determine it is a function the user wants to call, I iterate through the methods that the developer has decorated. <-- You meant that right?

    6) I then get the return value from the function, and write it out via the http context.

    Is that basically the idea?
    Yep

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to make my own custom "page" object?

    Quote Originally Posted by dbassettt74 View Post
    Okay, so I was able to do the following:

    Code:
    Imports Microsoft.VisualBasic
    Imports System.Reflection
    
    Public Class CustomPageClass
        Inherits System.Web.UI.Page
    
        <AttributeUsage(AttributeTargets.Method, Inherited:=True, AllowMultiple:=True)> _
        Public Class CustomAttribute
            Inherits System.Attribute
    
        End Class
    
        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim methods As MethodInfo() = Me.GetType.GetMethods
            For Each method As MethodInfo In methods
                Dim att As WebMethodAttribute = Attribute.GetCustomAttribute(method, GetType(CustomAttribute))
                If att IsNot Nothing Then
                    Response.Write(method.Name + "<br>")
                End If
            Next
        End Sub
    
    End Class
    Which gets all methods from derived class. I do see the method in there that I decorated with the custom attribute, but I'm wondering how to limit the results by GetMethods to just those methods that are decorated with a certain attribute?
    Not sure what you mean - aren't you doing that already by checking the methodattribute? You're looping through and inspecting each method in the class to see if it has your attribute on it

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to make my own custom "page" object?

    [QUOTE=mendhak;3666025]
    Yes, that's exactly what I'm talking about! But I'm still a little unclear here. Let me know if this is correct:

    1) I create a base class that defines one or more custom attributes. This base class handles all requests/responses from/to the web browser.


    My point is that they don't have to inherit from anything, the inheritance is a red herring/moot point. It won't make a difference. BBCode doesn't allow for a strikethrough. I tried [s]strike[/s] and [strike]strike[/strike] but can't do it.



    5) If I determine it is a function the user wants to call, I iterate through the methods that the developer has decorated. <-- You meant that right?



    Yep
    But if the developer has created a class that should be handled by my class, and he doesn't inherit from my class, how will my class know this? Also, how would the developer decorate anything if there is no custom attribute defined?

    Maybe I should make a little more clear what I'm trying to do. I'm basically trying to re-create my own "webservice", and trying to use the model that ASP.NET uses, unless you don't think that is good? When you create a web service with asp.net, it generates an ASMX page and corresponding VB code behind. The code behind, inherits from System.Web.Services.WebService. I believe that is what allows a request to the ASMX page to be parsed and then uses reflection to iterate through the VB code behind, to find any methods decorated with <WebMethod()>. I'm trying to do something like this. But I want to make it my own and do custom things with it.

    Visual Studio 2010

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to make my own custom "page" object?

    Your class code doesn't care because of this line of code.

    Dim methods As MethodInfo() = Me.GetType.GetMethods

    The GetMethods can occur from anywhere, so you could do it against any other class.

    Dim methods As MethodInfo() = SomeOtherClass.GetType().GetMethods()

    See what I mean? All you need is [CustomAttribute()] on methods inside SomeOtherClass.

    As for the developer knowing whether the attribute exists or not, that's a matter of referencing whatever assembly the CustomAttribute class is in.




    Take your webmethod as an example. WebMethodAttribute just happens to be in System.Web.Services but importantly, it inherits from Attribute. Web service codebehind classes inherit from System.Web.Services.WebService. Attributes are a completely different area of the .NET framework as you can see.

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to make my own custom "page" object?

    Okay, I see what you mean regarding the attribute. It is available to the developer by simply referencing my assembly, not necessarily inheriting from. But, if the developer does not inherit, I have no knowledge of the developer's class that he created. So how would I use the SomeOtherClass.GetType().GetMethods()? This assumes I know the name of the SomeOtherClass class.

    I'm slowly getting this but still a little fuzzy on the overall setup.

    This will be an ASP.NET website, that will not necessarily have any actual ASPX pages, because I think this is what the HttpHandler is for. I can have the handler route the request context to my custom class, right? And in that custom class, based on what the user requested, I will then iterate over the developer's classes, looking for particular decorations, do some processing, and then return result via "response" object.

    So say the requested URL (could be POST or GET) is:

    http://somesite.com/customprocessor/doSomething

    The HttpHandler, will capture this, and route to my custom class, and my custom class will see the path "/doSomething" and handle it appropriately.

    Is this possible? Can the ASP.NET "engine" be attached to this, even though the user is not explicitly including a file extension like .ASPX?

    Looks like someone else is trying to do the same thing, but never got a response:
    http://www.vbforums.com/showthread.php?t=379364
    Last edited by softwareguy74; Nov 26th, 2009 at 11:19 PM.

    Visual Studio 2010

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to make my own custom "page" object?

    I think the developer's class has to inherit from one of my classes, so that I know which class name to use when calling GetType.GetMethods. I just tried it without inheriting, and it doesn't return anything. This is what I would expect because I don't know the developer's class name. So I'm thinking this is why it must inherit, so that you're able to call SomeClassName.GetType.GetMethods, "SomeClassName" being my class that the developer inherits from. If the developer puts some arbitrarily named class such as "SomeCrazyClass", how will I know its name from my compiled dll?

    Visual Studio 2010

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: [RESOLVED] How to make my own custom "page" object?


    Visual Studio 2010

  15. #15
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to make my own custom "page" object?

    Quote Originally Posted by dbassettt74 View Post
    Okay, I see what you mean regarding the attribute. It is available to the developer by simply referencing my assembly, not necessarily inheriting from. But, if the developer does not inherit, I have no knowledge of the developer's class that he created. So how would I use the SomeOtherClass.GetType().GetMethods()? This assumes I know the name of the SomeOtherClass class.

    I'm slowly getting this but still a little fuzzy on the overall setup.

    This will be an ASP.NET website, that will not necessarily have any actual ASPX pages, because I think this is what the HttpHandler is for. I can have the handler route the request context to my custom class, right? And in that custom class, based on what the user requested, I will then iterate over the developer's classes, looking for particular decorations, do some processing, and then return result via "response" object.

    So say the requested URL (could be POST or GET) is:

    http://somesite.com/customprocessor/doSomething

    The HttpHandler, will capture this, and route to my custom class, and my custom class will see the path "/doSomething" and handle it appropriately.

    Is this possible? Can the ASP.NET "engine" be attached to this, even though the user is not explicitly including a file extension like .ASPX?

    Looks like someone else is trying to do the same thing, but never got a response:
    http://www.vbforums.com/showthread.php?t=379364
    Yes, that's possible. Because the HttpHandler is an interceptor of sorts, you could theoretically run an entire website from it based on the incoming requests. Look at the process pipeline here:



    Request comes in, modules handle it, then it's on to the handler before the page. So you can consider, just for this discussion, that ASPX is a very specific handler for a specific URL. HttpHandlers are generic handlers that are broader. Because it's before ASPX, you have the option of not calling an ASPX at all.

  16. #16
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to make my own custom "page" object?

    Further, if you're going to do extensionless URLs, then you may want to consider using IIS7, because IIS6 doesn't do extensionless URLs, unless you do a wildcard mapping on the site so that all requests go to the ASP.NET ISAPI DLL.

    As for the inheriting bit - do play around with it a bit to see what you can do.

    I'm also going to answer a question you asked earlier; I personally don't think it's a good approach because I feel that the benefits are marginal rather than substantial. But hey, you've got your reasons and justifications and that's probably another discussion.

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: [RESOLVED] How to make my own custom "page" object?

    Okay, well that concerns me a bit. I definitely want portability and I want to make my framework assuming the end user/developer does not have access to IIS administration functions. But at the same time, I was trying to make my framework very generic and not "page" based, because no pages will be served from this particular directory of the web site. That is the other key. Only a particular directory will process my framework. So, even if the developer places pages in /processor, they will never show. Only, if they place files in other directories of their site. So I really wanted to employ the use of extensionless URLs, but maybe I can't now?

    Visual Studio 2010

  18. #18
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] How to make my own custom "page" object?

    I suggest you experiment with it first. Create an HttpHandler that returns 'something'. Then in IIS 6 (not the built in web server), do a wildcard mapping. Then try

    http://machinename/yourapp/dosomething

    See if it gets hit. I believe this will work because it's also the way to get ASP.NET MVC to work in IIS6, but you lose performance.

  19. #19

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: [RESOLVED] How to make my own custom "page" object?

    Quote Originally Posted by mendhak View Post
    I suggest you experiment with it first. Create an HttpHandler that returns 'something'. Then in IIS 6 (not the built in web server), do a wildcard mapping. Then try

    http://machinename/yourapp/dosomething

    See if it gets hit. I believe this will work because it's also the way to get ASP.NET MVC to work in IIS6, but you lose performance.
    When you say "do a wildcard mapping", does that mean on the IIS server as an administrator, or within the web.config of the web app? As I mentioned before, I need to assume the developer does not have any access privileges to the IIS server configuration/administration.

    Visual Studio 2010

  20. #20

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: [RESOLVED] How to make my own custom "page" object?

    Quote Originally Posted by mendhak View Post
    I suggest you experiment with it first. Create an HttpHandler that returns 'something'. Then in IIS 6 (not the built in web server), do a wildcard mapping. Then try

    http://machinename/yourapp/dosomething

    See if it gets hit. I believe this will work because it's also the way to get ASP.NET MVC to work in IIS6, but you lose performance.
    Hmmm... That URL doesn't seem to work in II6. In the local ASP.NET development server, it works, correctly calling my custom HttpHandler to handle the request. But in IIS6, it simply tells me "404 not found". Probably because it thinks I'm looking for a directory named /dosomething, when in fact it doesn't actually exist.

    So maybe this is a problem? Again, I need to assume developer doesn't have access to IIS administration/configuration, beyond the web.config file. Is there maybe something else I can put in the web.config to make IIS6 handle this? This is what my HttpHandlers currently looks like:

    Code:
    <httpHandlers>
    	<remove verb="*" path="*.asmx"/>
    	<add verb="*" path="*" type="MyHttpHandler.Handler, MyHttpHandler"/>
    	<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    	<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    	<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
    </httpHandlers>

    Visual Studio 2010

  21. #21
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] How to make my own custom "page" object?

    Quote Originally Posted by dbassettt74 View Post
    When you say "do a wildcard mapping", does that mean on the IIS server as an administrator, or within the web.config of the web app? As I mentioned before, I need to assume the developer does not have any access privileges to the IIS server configuration/administration.
    Yes, on the IIS server as administrator. If the developer can't do it... tell them to get it done :P

  22. #22
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] How to make my own custom "page" object?

    Quote Originally Posted by dbassettt74 View Post
    Hmmm... That URL doesn't seem to work in II6. In the local ASP.NET development server, it works, correctly calling my custom HttpHandler to handle the request. But in IIS6, it simply tells me "404 not found". Probably because it thinks I'm looking for a directory named /dosomething, when in fact it doesn't actually exist.

    So maybe this is a problem? Again, I need to assume developer doesn't have access to IIS administration/configuration, beyond the web.config file. Is there maybe something else I can put in the web.config to make IIS6 handle this? This is what my HttpHandlers currently looks like:

    Code:
    <httpHandlers>
    	<remove verb="*" path="*.asmx"/>
    	<add verb="*" path="*" type="MyHttpHandler.Handler, MyHttpHandler"/>
    	<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    	<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    	<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
    </httpHandlers>
    When adding the wildcard mapping did you uncheck "Verify that file exists"?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width