locked
VWD2005 & HttpHandlers? RRS feed

  • Question

  • User-2003756693 posted

    I'm trying to get this article Rewriting the URL using IHttpHandlerFactory working on VWD2005.  I'm running it in the context of my local IIS on Windows XP and I put the MyPageFactory class in App_Code, but I can't get the debugger to stop on the code in MyPageFactory which implies to me it is not running it.  Can you tell me what I'm doing wrong?

    My goal is to write a web app that routes everything through /Default.aspx in the root while making it 100% transparent to the client thus allowing me to use the URL path to do a database look and load objects to prepare web pages like Ruby on Rails does.

    Any help would be greatly appreciated.

    Tuesday, December 26, 2006 2:10 AM

Answers

  • User390817712 posted

    Mike:

     I don't believe the tilde works in the path attribute for httpHandlers. Change your config to:

    <add verb="*" path="*.aspx" type="MyPageFactory, App_Code"/>

    and your handler will be invoked. You can use a <remove> element for subdirectories if you only want the handler to work in the root.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 26, 2006 3:17 PM

All replies

  • User390817712 posted

    Is the handler registered in web.config? Based on your other question, I'd suspect this is the problem :)

     

    Tuesday, December 26, 2006 11:50 AM
  • User-2003756693 posted

    Thanks for your reply. At first I had this within <system.web>:

       <httpHandlers>
          <add verb="*" path="~/*.aspx" type="MyPageFactory" />
       </httpHandlers>

    Based on your other answer, I tried this:

       <httpHandlers>
          <add verb="*" path="~/*.aspx" type="MyPageFactory,App_Code" />
       </httpHandlers>

    Sadly, neither work. :-(

    Any other suggestions?  I'll give you the full source.  My project is called TestHttpHandler:

    In App_Code as MyHttpHandler.vb, I have:

    1    Imports Microsoft.VisualBasic
    2    
    3    Imports System
    4    Imports System.IO
    5    Imports System.Web
    6    Imports System.Web.UI
    7    
    8    Public Class MyPageFactory
    9        Implements IHttpHandlerFactory
    10   
    11       Public Function GetHandler( _
    12           ByVal context As HttpContext, _
    13           ByVal requestType As String, _
    14           ByVal url As String, _
    15           ByVal pathTranslated As String) As IHttpHandler Implements IHttpHandlerFactory.GetHandler
    16           context.Items("fileName") = Path.GetFileNameWithoutExtension(url).ToLower()
    17           Return PageParser.GetCompiledPageInstance(url, context.Server.MapPath("~/Test.aspx"), context)
    18       End Function
    19   
    20       Public Sub ReleaseHandler(ByVal handler As IHttpHandler) Implements IHttpHandlerFactory.ReleaseHandler
    21       End Sub
    22   
    23   End Class
    

     I have a Text.aspx file:

    1    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Test.aspx.vb" Inherits="_Default" %>
    2    
    3    <script runat="server">
    4       Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    5          TheLiteral.Text = HttpContext.Current.Items("fileName")
    6       End Sub
    7    </script>
    8    
    9    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    10   
    11   <html xmlns="http://www.w3.org/1999/xhtml" >
    12   <head runat="server">
    13       <title>Foo</title>
    14   </head>
    15   <body>
    16       <form id="form1" runat="server">
    17       <div>
    18             <p>Request was for: <asp:Literal ID="TheLiteral" runat="server" /></p>
    19       </div>
    20       </form>
    21   </body>
    22   </html>
    
    Test.aspx.vb is unchanged:
    1    Partial Class _Default
    2        Inherits System.Web.UI.Page
    3    
    4    End Class
    
    My web.config looks like this:
     
    
    1    <?xml version="1.0"?>
    2    <!-- 
    3        Note: As an alternative to hand editing this file you can use the 
    4        web admin tool to configure settings for your application. Use
    5        the Website->Asp.Net Configuration option in Visual Studio.
    6        A full list of settings and comments can be found in 
    7        machine.config.comments usually located in 
    8        \Windows\Microsoft.Net\Framework\v2.x\Config 
    9    -->
    10   <configuration>
    11   	<appSettings/>
    12   	<connectionStrings/>
    13   	<system.web>
    14   		<!-- Custom HttpHandler -->
    15   		<httpHandlers>
    16   			<add verb="*" path="~/*.aspx" type="MyPageFactory,App_Code"/>
    17   		</httpHandlers>
    18   		<!-- 
    19               Set compilation debug="true" to insert debugging 
    20               symbols into the compiled page. Because this 
    21               affects performance, set this value to true only 
    22               during development.
    23   
    24               Visual Basic options:
    25               Set strict="true" to disallow all data type conversions 
    26               where data loss can occur. 
    27               Set explicit="true" to force declaration of all variables.
    28           -->
    29   		<compilation debug="true" strict="false" explicit="true"/>
    30   		<pages>
    31   			<namespaces>
    32   				<clear/>
    33   				<add namespace="System"/>
    34   				<add namespace="System.Collections"/>
    35   				<add namespace="System.Collections.Specialized"/>
    36   				<add namespace="System.Configuration"/>
    37   				<add namespace="System.Text"/>
    38   				<add namespace="System.Text.RegularExpressions"/>
    39   				<add namespace="System.Web"/>
    40   				<add namespace="System.Web.Caching"/>
    41   				<add namespace="System.Web.SessionState"/>
    42   				<add namespace="System.Web.Security"/>
    43   				<add namespace="System.Web.Profile"/>
    44   				<add namespace="System.Web.UI"/>
    45   				<add namespace="System.Web.UI.WebControls"/>
    46   				<add namespace="System.Web.UI.WebControls.WebParts"/>
    47   				<add namespace="System.Web.UI.HtmlControls"/>
    48   			</namespaces>
    49   		</pages>
    50   		<!--
    51               The <authentication> section enables configuration 
    52               of the security authentication mode used by 
    53               ASP.NET to identify an incoming user. 
    54           -->
    55   		<authentication mode="Windows"/>
    56   		<!--
    57               The <customErrors> section enables configuration 
    58               of what to do if/when an unhandled error occurs 
    59               during the execution of a request. Specifically, 
    60               it enables developers to configure html error pages 
    61               to be displayed in place of a error stack trace.
    62   
    63           <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
    64               <error statusCode="403" redirect="NoAccess.htm" />
    65               <error statusCode="404" redirect="FileNotFound.htm" />
    66           </customErrors>
    67           -->
    68   	</system.web>
    69   </configuration>
    Lastly, when I navigate to http://localhost/TestHttpHandler/FooBar.aspx I get the following when I expected it to run the handler and load in the background:

    Server Error in '/TestHttpHandler' Application.


    The resource cannot be found.

    Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

    Requested URL: /TestHttpHandler/FooBar.aspx


    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210
    Thanks for your help!
     
    Tuesday, December 26, 2006 2:01 PM
  • User390817712 posted

    Mike:

     I don't believe the tilde works in the path attribute for httpHandlers. Change your config to:

    <add verb="*" path="*.aspx" type="MyPageFactory, App_Code"/>

    and your handler will be invoked. You can use a <remove> element for subdirectories if you only want the handler to work in the root.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 26, 2006 3:17 PM
  • User-2003756693 posted

    Thank you!  That was strange, as the article used the tilde ("~").

    Anyway, I got the debugger to breakpoint inside the handler, and it does what I expected.  It loads Test.aspx no matter what URL I send it as long as that URL ends with an .aspx, which is what I expected. Yea!

    But it won't run the code in Sub Page_Load(). I tried moving to the _Default class in the page behind code, but that didn't work either.  I moved back to the .aspx file and put the code by itself using <% and %> delimiters w/o the "Sub" and "End Sub" declarations and that *did* work.  But isn't it better to use the Page_Load(), and also why wouldn't it run Page_Load()?

    Regarding these issues, how can one debug syntax problems in the web.config file, and how can one debug problems like the fact Page_Load() won't run?  Do you just have to know what to put there? (if yes, that's awfully frustrating, no?)

    Lastly, do you know if it is possible configure IIS on Windows XP to support the Wildcard application maps?  I'm trying to set up URL rewriting such that file extensions are not required on URLs, and I want to be able to debug on my localhost (I don't want to install VWD on my server.) 

    Thanks so much for your help!

    Tuesday, December 26, 2006 5:08 PM
  • User390817712 posted

    But it won't run the code in Sub Page_Load(). I tried moving to the _Default class in the page behind code, but that didn't work either.  I moved back to the .aspx file and put the code by itself using <% and %> delimiters w/o the "Sub" and "End Sub" declarations and that *did* work.  But isn't it better to use the Page_Load(), and also why wouldn't it run Page_Load()?

    Regarding these issues, how can one debug syntax problems in the web.config file, and how can one debug problems like the fact Page_Load() won't run?  Do you just have to know what to put there? (if yes, that's awfully frustrating, no?)

    Yes, web.config can be frustrating and hard to debug. It's often just the case of knowing what to try and what can go wrong.

    For the Page_Load problem, I noticed you have AutoEventWireup="false" in the @ Page directive. This means you'll have to wire up the event handlers manually, which might be what you are missing. I have a post on this topic: http://odetocode.com/Blogs/scott/archive/2006/02/16/2914.aspx

      

    Lastly, do you know if it is possible configure IIS on Windows XP to support the Wildcard application maps?  I'm trying to set up URL rewriting such that file extensions are not required on URLs, and I want to be able to debug on my localhost (I don't want to install VWD on my server.) 

    Yes, wildcard application maps are possible on XP.

    Wednesday, December 27, 2006 10:05 AM
  • User-2003756693 posted

    Thank you, Scott. You've been great and I really appreciate your help.  I'm far less frustrated now. :-)

    Yes, web.config can be frustrating and hard to debug. It's often just the case of knowing what to try and what can go wrong.

    I've been thinking a lot lately about how Microsoft could improve things.  One thing would be to provide a web.config validator/debugger. The former would be an option at debug time to scan the web.config for errors and report the problems, and the second would allow you to set "alert points" that alert you when they are not run.

    For the Page_Load problem, I noticed you have AutoEventWireup="false" in the @ Page directive. This means you'll have to wire up the event handlers manually, which might be what you are missing. I have a post on this topic: http://odetocode.com/Blogs/scott/archive/2006/02/16/2914.aspx

    It's so obvious, now that you mention it! :-(  Doh!  But similarly, it would be great if Visual Studio would sent that I have a breakpoint in a function that won't be run because of AutoEventWireup and instead put up a warning telling me what I need to do to fix it.  Doesn't that sounds like a good idea to get more newbies productive with Microsoft technologies (as opposed to them moving to Linux technlogies?)

     Yes, wildcard application maps are possible on XP.

    Any guidance on how to set it?  It's not available within the MMC dialog.

    Thursday, December 28, 2006 1:25 AM
  • User390817712 posted

    Mike:

    In XP I think you have to enter ".*" as the extension (without the quotes) to get a wildcard mapping and have the OK button enable itself. See: http://support.microsoft.com/?id=317948 if you have some problems. There are some screenshots on this page: http://www.webdavsystem.com/documentation/creating_webdav_web_app. This is setting up WEBDAV, but the steps are the same.

    Yes, there are a lot of things Microsoft could do to make thier tools easier to use. They could be more proactive in trying to identify common problems that we run into. There are some tools (like FxCop and the ASP.NET Best Practice analyzer), but there could be a lot more. It's a good idea.

    Thursday, December 28, 2006 10:57 AM