Tarun Kohli

Page Redirects using Spring.NET

Who is responsible for page redirects in ASP.NET MVP – The View or the Presenter ?

None of the above, it is you 🙂

On a serious note, it is the View as one shouldn’t pollute the Presenter with the page navigation logic. Having Page Redirection logic embedded in the Presenter completely defeats the purpose of the MVP design pattern itself. Thus, we started wiring the page redirects in the View, only to realize it was not very clean to have the .aspx page names embedded inside the code behind file.

Fortunately, we were already using Spring.NET for our DI needs and it turned out that there was a way in it to externalize the page redirects. It has the concept of defining the <Result> tag which helps in externalizing the page redirect logic. I followed the steps outlined in their documentation but it didn’t work. It gave me an exception of destination not found. I debugged the Spring.NET assemblies to figure out that it was not even loading the <Results> collection.

I still haven’t been able to figure out the problem but serendipitously found the solution. First of all, the document way of setting up Redirects. The documentation describes the following process to set up page redirects.

1. Inherit your page from Spring.Web.UI.Page

2. Setup the Result configuration in the objects section of your config file

<objects>

<object type="~/UI/Forms/UserRegistration.aspx" parent="basePage">
  <property name="UserManager">
    <ref object="userManager"/>
  </property>

  <property name="Results">
    <dictionary>
      <entry key="userSaved" value="redirect:UserRegistered.aspx?user=${UserInfo}"/>
      <entry key="cancel" value-ref="homePageResult"/>
    </dictionary>
  </property>
</object>

</objects>

 

3. Raise the Event to let Spring.NET know which Result was set

private void SaveUser(object sender, EventArgs e)
{
    UserManager.SaveUser(UserInfo);
    SetResult("userSaved");
}

 

This should ideally redirect it to the UserRegistered page in the above example. But, unfortunately, it didn’t work for me. As I mentioned above, it kept on giving me an exception that destination was not found. The thing that worked for me was –

1. Inherit the asp.net page from Spring.Web.UI.Page

2. Setup the Spring.NET section in the web.cofing file as –

<sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
      <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
      <section name="typeAliases" type="Spring.Context.Support.TypeAliasesSectionHandler, Spring.Core"/>
</sectionGroup>

 

Please note that I took out the objects section name.

3. Create a web.xml file to define all the redirects

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <!-- Referenced by main application context configuration file -->
  <description>
    The web layer definitions
  </description>

  <object type="~/Resources/MyProfile.aspx">
    <property name="Results">
      <dictionary>
        <entry key="userSaved" value="redirect:UserRegistered.aspx" />
      </dictionary>
    </property>
  </object>
</objects>

 

4. Setup the Spring context in the web.config file as –

  <spring>
    <context>
      <resource uri="~/Config/Presenters.config" />
      <resource uri="~/Config/Services.config" />
      <resource uri="~/Config/Web.xml"/>
     </context>
  </spring>

 

5. Raise the event –

private void SaveUser(object sender, EventArgs e)
{
    UserManager.SaveUser(UserInfo);
    SetResult("userSaved");
}

 

I’m not sure why but it just works. The only difference in the documented approach and this one is that there is a separate web.xml file defined in the context section to keep the redirects and there is no separate objects section in the configuration file.

 

Related Articles

#Tech

NHibernate, Linq and Lazy Collections

For the past few months we have been busy building a services framework for one of our clients so that they can build all of their enterprise web services without ever having to worry about the cross cutting concerns and... Read more
#Tech

Page Redirects using Spring.NET

Who is responsible for page redirects in ASPNET MVP – The View or the Presenter None of the above, it is you :) On a serious note, it is the View as one shouldn’t pollute the Presenter with the page navigation... Read more