Michael Flanakin's Web Log

Comments and complaints on software and technology in general

  Home  |   Contact  |   Syndication    |   Login
  159 Posts | 18 Stories | 89 Comments | 530 Trackbacks

News

This weblog is no longer being maintained. For the latest, check out www.michaelflanakin.com!

Article Categories

Archives

Post Categories

Image Galleries

Miscellaneous

Don't you hate it when you can't find an answer to your problems after searching Google? I know I do. I've been trying to get the Front Controller pattern (implementation) working, but seem to be having a lot of issues. Hopefully, others will run across this post instead of wading through the hundreds of dead-end forum posts and walk-throughs. I'm sure there is one place that answers all my questions, but as of now, I'm left to the wonderful world of Google.

Anyway, while working on this solution, I ran into several problems. All of which seem to be common. Hopefully, I can try to knock them out one-by-one...

Configuration Sections

This wasn't as big of a deal as the rest of the areas, but I wanted to touch on it to help ease some pain for anyone else going through these hassles.

The pattern implementation suggested adding a controller.mapping section to the Web.config file. I would advise against this approach. While I do not see anything fundamentally wrong with it, it caused me a bit of headache when I first attempted it - this might have been because I was trying a few different things with it. For the sake of simplicity, what I did was reused what was built into ASP.NET: System.Configuration.NameValueSectionHandler.

Here's the config and mapping sections that I chose to use. Feel free to change the names as needed, but remember what you change them to - you will need to reference them in code.

<configSections>
  <sectionGroup name="controllerSettings">
    <section
     name="urlMappings"
     type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </sectionGroup>
</configSections>

<controllerSettings>
  <urlMappings>
     <add
      key="/Virtual/FakePage.aspx"
      value="~/RealPage.aspx" />
  </urlMappings>
</controllerSettings>

The reason I changed some of the names is because I'm hoping to expand the controller capabilities later. The most important thing to note here is that the type attribute of the <section> element must have the version, culture, and public key token in order to work. At first, I abided by the guidance and attempted to use my own section handler, but to no avail. I'm sure it isn't hard to get this to work, but right now I'm just interested in a proof of concept and basis for future work; hence, reusing built-in classes.

Oh, and, in case you're wondering, I'm using .NET v1.1. Check your machine.config (C:\WINNT\Microsoft.NET\Framework\v###\CONFIG) to see what appropriate values might be for your system.

HTTP Handlers

HTTP handlers respond to ALL requests within the ASP.NET environment. The pattern implementation specifically covers .aspx extensions, and I will stick with that, for now. First off, when I say that the handler responds to all requests, I am including the Server.Transfer() redirects, as well. This is important to remember later.

When setting up the handler within the Web.config file, remember that the wildcard path that you use cannot apply to the true path to the file. For instance, the following does not work...

<system.web>
  <httpHandlers>
    <add
    
verb="*"
   
 path="*.aspx"
   
 type="Indigo.Web.HttpHandler,Indigo.Web" />
  </httpHandlers>
</
system.web>

The problem is that when the fake page is transferred to the real page, the real page also abides by this path. So, what you should use is something that is specific to your fake page names (like “Fake*.aspx“). If you don't, you will end up getting a blank page or an error message (see next section).

I'm sure that there are a few better ways to accomplish this; but, I just want it to work for right now. If anyone knows of better ways to do it, please let me know.

Server.Transfer() or Context.RewritePath()

I've seen a lot of discussion about Context.RewritePath(), but most of this is implemented within the Application_BeginRequest() method, which does not seem like a sound practice to me. I may be wrong, but I would need to look into it more to understand it better. As for now, tweeking the Web.config seemed to get my Server.Transfer() to work - I was getting the following error (where “FakePage” is the requested page):

Error executing child request for FakePage.aspx

I was looking all over for an answer to this error, but to no avail. I can't remember where I ran across the HTTP handler idea, but that seemed to fix the problem.

Right now, I'm wondering which solution is better: Server.Transfer() or Context.RewritePath(). The rewrite seems to be a little odd as far as how it works, but I didn't care enough to dig into it more than to see what it was. After getting the transfer to work, I pretty much just put everything down. If anyone knows of any pros/cons for using one or the other, please let me know.

Passing QueryStrings to the Real Page

After setting things up, I wanted to send the FakePage.aspx to RealPage.aspx?Show=Section1. The problem seems to be that I can't read the QueryString on the real page. I placed the entire URL in the Web.config file, but for some reason that didn't work. It's almost as if the transfer ignores it. If I use the QueryString in the fake page, then everything works fine.

I'm still working on this, but am not sure when I'll get a chance to finish things up. I'm trying to avoid custom code to fix the QueryString problem. I'd like to have configuration information be good enough, if possible. I'm sure I'll figure something out.

Please let me know if you find any issues of your own when attempting to implement this pattern (or any others). Hopefully, I can attack those in round 2, when I finish things up - that will probably be next weekend. We'll see.

posted on Sunday, February 01, 2004 8:49 PM

Feedback

# re: Implementing the Front Controller Pattern (1.0) 2/4/2004 9:16 AM Mark
Hi, this blog is helpful. I hadn't known of another way of handling the controller.mapping config code. Thanks for that tip.

I'm in the process of wrestling with the Front Controller implementation code, and have an issues to ask you about, and a resource page regarding an issue you are wondering about.

You mentioned I'm wondering which solution is better: Server.Transfer() or Context.RewritePath()? Here is Microsoft resource page that may help: http://support.microsoft.com/default.aspx?scid=kb;en-us;817036 It's Microsoft's acknowledgement that server.Transfer contains a bug when called in this way in ASP.Net.

I'm having a problem with the following line: Command command = new UnknownCommand(); My code errors in CommandFactory class on this line. - Any insights into this would be appreciate.


Mark Collins
Washington State Department of Ecology
marc461@ecy.wa.gov


# re: Implementing the Front Controller Pattern (1.0) 2/10/2004 7:43 PM Michael Flanakin
I'm thinking about creating a how to guide on implementing front controller-like functionality. As I started to delve into this, however, I started thinking that perhaps a more general guide to choosing the best solution might be good. Then, my research brought me to places that I didn't even care about. So, I'm struggling with the worth of my effort. If anyone has interest in an article that talks about choosing modules vs. handlers vs. handler factories and/or transfer vs. rewrite path, please let me know. The more input I receive, the more likely I am to finish it. It looks like it might be a 3-parter. For now, however, it's on hold.

Mark,
I didn't implement the UnknownCommand. Actually, I strayed away from how the Microsoft implementation detailed it. I'm trying to create an abstract handler that people can add into any application and have it automatically work. So, for my case, I treated everything as a RedirectingCommand. I already know that there are things that probably need to change. If I ever finish it, and my articles (see above), then I will include source code that describes my solution. Back to your issue, I'd have to see your code to know more.

# re: Implementing the Front Controller Pattern (1.0) 2/17/2004 5:56 PM Jeegar
I was doing similar efforts of writing FC framework.
I come across the problem where asp:button control results in self post form code in html essentially exposing my RealPage.
What I want to achieve is that my html form post must post to the common Controller page (hiding the actual page) or to FakeCommand1.aspx or so.

Any ideas ?

# re: Implementing the Front Controller Pattern (1.0) 3/1/2004 2:39 AM gagan
Hi
I am also trying to implement front controller pattern. Can anyone tell me how to pass the form data to the next page e.g. User fills the data in the form. Then in the submit button handler of that form, i do the response.Redirect to a fake page. From that fake page control will go to Handler. I have to access the form data in the command object. If i pass by query string it works. I do not want to use querystring. What is other alternative?

Thanks in advance
Regards
gagan


# re: Implementing the Front Controller Pattern (1.0) 3/2/2004 7:56 PM nas
Using Server.Transfer in the HttpHandler always resulted in an error for me. Can you please post a link or the code to fix this problem. Thanks.

# re: Implementing the Front Controller Pattern (1.0) 3/4/2004 3:09 AM gagan
I am also facing problem in using Server.Transfer. If i use Response.Redirect i am not able to pass data using Context.Items collection. Can you please post a link or the code to fix this problem. Thanks.


# re: Implementing the Front Controller Pattern (1.0) 3/18/2004 6:24 AM Chris Conboy
I had problems getting the implementation of this pattern to run as well. Great discussion here, btw. While looking up some related info, I stumbled on this MS topic, "ASP.NET and Struts: Web Application Architectures", in which there is a link to an install for a 'working' Front Controller sample.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/ASPNet-ASPNet-J2EE-Struts.asp

Direct link:

http://download.microsoft.com/download/2/4/a/24abd4b4-1319-4bf8-bbfc-6cb269bf8625/frontcontrollerdemo.msi

or Google for
frontcontrollerdemo.msi

This sample needed some very minor finessing from me to get it to work. The sample assumes you will install it in the default IIS website, which I did not do.

I wonder if there are more hidden goodies like this around the MS download site.


# re: Implementing the Front Controller Pattern (1.0) 3/31/2004 4:10 AM MOnrozies Pierre jean
Hello,

thanks for the frontControllerDemo.msi sample it helps in understanding how to do. Did your version miss the page 3 to 7 referenced in the web.config and also the default aspx page?

I found it useful to follow the theory but I am unable to understand why in this implementation the pages don't inherit from the RedirectingCommand, any Ideas?

thanks and have a nice day,

pierre jean

# re: Implementing the Front Controller Pattern (1.0) 3/31/2004 4:49 AM Monrozies Pierre jean
hello,

the page don't miss they exist only in the web.config and serve only for navigation sample to illustrate redirection or parameters using.

they don't inherit the RedirectingCommand because ther is a RedirectAction which does and from which page are descendant.

thanks

# re: Implementing the Front Controller Pattern (1.0) 4/29/2004 5:52 PM Guru
Hello Michael,

You have not mentioned how you solved the Server.Transfer issue...Or am I not reading properly..

Help plz
Guru

# re: Implementing the Front Controller Pattern (1.0) 5/13/2004 1:19 PM pierre jean Monrozies
hello Michael,

this is my web config Handler section :
<httpHandlers>
<add verb="*" path="*Action*.aspx" type="CarcassonPost.FrontController.FrontControllerHandler, CarcassonPost"/>
</httpHandlers>

but I still have Error executing child request for MyPageAction.aspx

when I use Server.Transfer().

What do you do to solve the problem.

thanks you



# re: Implementing the Front Controller Pattern (1.0) 6/7/2004 5:43 AM sourabh
Hi
I am able to get everything working with Frontcontroller except PostBack events.
I want to handle my postback event in the actual .aspx page and i want this to pass thru myhttpHandler.processrequest()
i.e. first my custom hadnler is called and then it passes the request to my .aspx page with IsPostBack still true.
If i do Server.transfer from my myhttpHandler.processrequest() the IsPostBack on the .aspx is "false".
Another option
In application_beginrequest Is I do context.rewritepath(my.aspx) but this does not invoke myHttphandler.

Any Ideas
Sourabh

# re: Implementing the Front Controller Pattern (1.0) 6/25/2004 3:10 AM Sameer
After a lot of search on net I have found the way to change my web.config. Plz read this and tell me it is right way to construct front controller or not. I'm writing my code of controler in a class library and then using it's dll file in new folder in wwwroot. Here I'm writing a file web.config and by editing this I'm able to access the controller code.
The url for this example is

http://support.microsoft.com/default.aspx?scid=kb;en-us;308001&Product=NETFrame

# IMPLEMENTED Front Controller Pattern (1.0) 7/30/2004 6:08 AM Xishan
I have made a very effective and functional Front Controller that has almost all the functionalities as that of Java Struts.
If u have any questions regarding this matter plz mail me.
I will be happy to reply.
Thanks
Xishan

# re: Implementing the Front Controller Pattern (1.0) 9/1/2004 8:06 PM Ravi Somepalli
I disabled the view state and my problems with Server.Transfer disappear - See if you can get rid of some of yours with this

<pages buffer="false"
enableSessionState="true"
autoEventWireup="true"
smartNavigation="true"
enableViewState="false"
enableViewStateMac="false"/>

# re: Implementing the Front Controller Pattern (1.0) 9/23/2004 4:20 PM Param
Here's the solution:

http://support.microsoft.com/default.aspx?scid=kb;en-us;821758

# re: Implementing the Front Controller Pattern (1.0) 11/24/2004 9:36 PM Shyam S
For getting the Request.Form collection in another page while redirecting to it when we click asp:button, add a javascript in the button click which changes the __viewstate name.
In Form_Load event add
btnSave.Attributes.Add("onclick","submitForm('nextForm.aspx');");
Then in the aspx file add javascript :-
function submitForm(sNewFormAction)
{

document.forms[0].action = sNewFormAction;
document.forms[0].__VIEWSTATE.name = 'NOVIEWSTATE';
}

Try this.

# re: Implementing the Front Controller Pattern (1.0) 11/25/2004 3:18 PM ltcmelo
For some reason i don`t know why, the Front Controller demo doesn`t work properly at the first time it`s executed. If i start debugging for the second time (without closing visual studio) everything goes fine.
Anyone knows why?

# re: Implementing the Front Controller Pattern (1.0) 12/3/2004 11:50 AM ltcmelo
I installed the service pack 1, but server.transfer still doesn`t work. Any ideas???

# re: Implementing the Front Controller Pattern (1.0) 2/21/2005 1:41 PM danny
Take a look at http://lattis.sourceforge.net/

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 2 and 5 and type the answer here: