Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET

ASP.NET Page Navigation Using Workflow 4.0

Rate me:
Please Sign up or sign in to vote.
4.70/5 (46 votes)
21 Feb 2011CPOL4 min read 130K   5.3K   88   46
This tutorial demonstrates how Workflow 4.0 can be used for page navigation in ASP.NET.

Introduction

In this article, I will explain how Workflow 4.0 can be used to control page navigation in ASP.NET. If you are new to Workflow 4.0, please go through the MSDN documentation to understand how it works since the scope of this article is limited to workflow creation.

Background

Technology used for this sample: .NET 4.0, Visual Studio 2010, Workflow 4 (part of .NET 4 Framework), ASP.NET Web Application.

Objective: To remove page navigation logic from .aspx pages and centralize it in a workflow module.

Example: Page navigation is implemented for Boston Dealers who sell Cars and Motor cycles. Please download the attached sample application to understand this article completely.

Page Navigation Using Workflow 4.0

The below diagram depicts the page navigation logic which is implemented for Boston dealers:

Flow1.jpg

Classes Inside the Sample Application

  1. "BostonWorkflow.xaml" contains the workflow required for page navigation. If you have a website model instead of a web application, you need to add the .xaml file in a class library and reference it / you can load the XAML file using ActivityXamlServices.Load().
  2. "BasePage.cs" is a convenience class which acts as a base page for all the web pages. It has a workflow wrapper method called GotoNextPage which invokes the workflow with the required parameters and redirects the browser to the desired page.
  3. "Customer.cs" is a simple customer class which stores customer information and vehicle information. The instance of this class is passed to the workflow to make decisions.
  4. "CreditScore.cs" is used to calculate the credit score of a customer if she/he applies for a loan. In the real world, it will be a Web Service call.
  5. In all web pages, the GotoNextPage() method is used instead of Response.Redirect.

Steps to be Followed to Create the Page Navigation Workflow

Step 1

For designing a workflow, we need to choose "Add-->New Item-->Worfklow -->Activity" class.

Step 2

Arguments have to be setup for the workflow to pass In / Out parameters. (Consider the workflow as a method which needs input parameters to make a decision and a return parameter to return the result.)

In this workflow, I have setup three arguments:

ArgNameType Direction Description
currentPagestringInwhich tells from where the workflow is called.
nextPage string In / Out return parameter to tell the next page.
customer Customer In / Out instance of the customer object.

The scope of these arguments is for the entire workflow:

Argument.jpg

Step 3

Even though the page flow (above diagram) looks like a continuous flowchart, it gets broken whenever the next page is determined. So the "Control flow" is chosen to orchestrate the workflow rather than the "flowchart". Inside the Control flow, I decided to use Switch<T> to switch the control based on the currentPage argument.

workflow.jpg

Step 4

Inside the "case", you can write business logic using conditions such as "If Else", "For Each" which are available in the control flow section of the toolbox, and the defined arguments can be used inside the conditions to perform checks or assign a value.

In this diagram, the customer argument is used to check the vehicle type and the result is assigned to nextPage. For assigning the result, we need to use the Assign control from the primitive section.

WorkflowHome.jpg

Step 5

Apart from having simple "if / then" conditions, we can also have a Flowchart control inside the case statement to determine complex logic. In the below diagram, the customer's credit score is checked if their annual income is less than 50000. To check the credit score, the external method in CreditScore.cs is called using the InvokeMethod control in primitives.

WorkflowPayment.jpg

Variables can be declared for a certain scope of the workflow like C# programming. In this sample, two variables named customerCreditScore and creditScore are used to store the value and the instance.

Variable.jpg

Also, the customer.Interest value is set based on the BL and it is displayed in Delivery.aspx.

Step 6

Now the workflow has to be wired up to the ASPX pages. In this sample, I have created a class called BasePage which invokes the workflow class and passes the required arguments.

WorkflowInvoker.Invoke invokes the workflow with the arguments which we defined while orchestrating the workflow.

C#
WorkflowInvoker.Invoke(new BostonWorkflow(), wfParams); 

Arguments (In/Out) have to be passed as a Dictionary object:

C#
IDictionary<string, object> wfParams = new Dictionary<string, object>();
wfParams.Add("currentPage", Request.FilePath.Substring(1, Request.FilePath.Length - 1));
wfParams.Add("customer", customer);  

The return value nextPage will get created in the workflow and gets added to the Dictionary. Using this, we can redirect the application to the desired page.

C#
Response.Redirect(wfParams["nextPage"].ToString());

BasePage.cs

C#
public void GotoNextPage()
{
   IDictionary<string, object> wfParams = new Dictionary<string, object>();
   wfParams.Add("currentPage", Request.FilePath.Substring
			(1, Request.FilePath.Length - 1));
   wfParams.Add("customer", customer);                           
   wfParams = WorkflowInvoker.Invoke(new BostonWorkflow(), wfParams);
   Response.Redirect(wfParams["nextPage"].ToString());
} 

For all the web pages which implement BasePage, calling GotoNextPage will do the job of redirection. By doing this, we remove the navigation logic from the pages and centralize it in the workflow.

HomePage.aspx.cs

C#
protected void btnNext_Click(object sender, EventArgs e)
{
   customer.vehicle.VehicleType = RadioButtonList1.SelectedValue;
   GotoNextPage();
}

Conclusion

Thanks for reading the article. Please download the attached sample and experiment with it yourself.

History

  • 21 February, 2011: Initial post.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Developing software since 2000.

Comments and Discussions

 
QuestionHow can we add state mgmt and make this sample work with sql persistence store Pin
zensim20-Nov-13 9:01
zensim20-Nov-13 9:01 
Suggestionsome suggestions Pin
LoveJenny11-Jul-13 15:51
LoveJenny11-Jul-13 15:51 
GeneralRe: some suggestions Pin
Sridhar Subramanian17-Aug-13 5:06
Sridhar Subramanian17-Aug-13 5:06 
QuestionMy Vote of 5 Pin
dennisigah27-Mar-13 9:24
professionaldennisigah27-Mar-13 9:24 
GeneralMind Blowing Article Pin
israrali19-Feb-13 19:25
israrali19-Feb-13 19:25 
GeneralMy vote of 5 Pin
Atal Upadhyay31-Jan-13 23:38
Atal Upadhyay31-Jan-13 23:38 
GeneralMy vote of 5 Pin
toantvo26-Jan-13 2:24
toantvo26-Jan-13 2:24 
QuestionWork flow page navigation Pin
Shivarajbk1-Jan-13 20:09
Shivarajbk1-Jan-13 20:09 
GeneralMy vote of 5 Pin
Savalia Manoj M1-Jan-13 19:39
Savalia Manoj M1-Jan-13 19:39 
GeneralMy vote of 5 Pin
csharpbd14-Nov-12 1:07
professionalcsharpbd14-Nov-12 1:07 
GeneralMy vote of 1 Pin
Syed J Hashmi4-Aug-12 12:22
Syed J Hashmi4-Aug-12 12:22 
GeneralMy vote of 5 Pin
PotatoJam9-Apr-12 20:43
PotatoJam9-Apr-12 20:43 
GeneralMy vote of 4 Pin
c27bharti1-Mar-12 0:07
c27bharti1-Mar-12 0:07 
Questionexcellent article Pin
azure991-Feb-12 12:46
azure991-Feb-12 12:46 
Questionmonitoring workflow instance Pin
aa.azizkhani11-Aug-11 5:43
aa.azizkhani11-Aug-11 5:43 
AnswerRe: monitoring workflow instance Pin
Sridhar Subramanian12-Aug-11 0:55
Sridhar Subramanian12-Aug-11 0:55 
QuestionRe: monitoring workflow instance Pin
aa.azizkhani12-Aug-11 4:19
aa.azizkhani12-Aug-11 4:19 
GeneralMy vote of 2 Pin
arviy30-Apr-11 21:33
arviy30-Apr-11 21:33 
GeneralRe: My vote of 2 Pin
Ryan Minor27-Dec-12 4:16
Ryan Minor27-Dec-12 4:16 
GeneralRe: My vote of 2 Pin
arviy10-Jan-13 2:21
arviy10-Jan-13 2:21 
GeneralMy vote of 5 Pin
Aaron Hanusa24-Apr-11 3:48
Aaron Hanusa24-Apr-11 3:48 
Very nice, thanks for sharing.
GeneralThanks for sharing Pin
Abdul Rhman AbuMaree28-Mar-11 21:07
Abdul Rhman AbuMaree28-Mar-11 21:07 
GeneralMy Vote of 5 Pin
RaviRanjanKr10-Mar-11 16:31
professionalRaviRanjanKr10-Mar-11 16:31 
GeneralRe: My Vote of 5 Pin
Sridhar Subramanian11-Mar-11 17:16
Sridhar Subramanian11-Mar-11 17:16 
GeneralMy vote is 5 Pin
Sunasara Imdadhusen7-Mar-11 22:42
professionalSunasara Imdadhusen7-Mar-11 22:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.