Skip to content

Auto Redirect Overview

mikeobrien edited this page Sep 14, 2010 · 2 revisions

Sometimes the result of a web service call will be a redirect. This is especially true when the service is used by a 3rd party who desires to have a custom result page to maintain identity and branding. The WcfRestContrib.ServiceModel.Description.RedirectAttribute can be applied to an operation contract to automatically redirect the response to a url specified as a querystring parameter. For example, lets say that our library offers a resource that enables users to add book information to the system. An affiliate library would like to offer the ability to add book information to our system but retain their branding and identity throughout the process. We expose the following resource for this purpose with the option of adding a “redirecturl” querystring parameter to redirect to a custom thank you page:

http://services.neilsbohrlibrary.com/books?redirecturl=http%3A%2F%2Fwww.maxplancklibrary.com%2FAddBookThankyou.html

In our service contract we would define an operation contract with a querystring parameter defined for the redirect url. We also apply the RedirectAttribute and pass the name of the querystring parameter that will contain the url to redirect to:

[WebInvoke(UriTemplate = "/?redirecturl={redirectUrl}", Method=Verbs.Post)]
[OperationContract]
[Redirect("redirecturl")]
void AddBook(Book book, string redirectUrl);

The redirect url parameter can be ignored in the method implementation as its only required to define the UriTemplate. If a redirect url is specified, the redirect status and location header will be automatically set on the response.

NOTE: Under certain circumstances all querystring parameters will need to be passed due to a limitation of the WCF REST API. If the querystring parameter needs to be omitted it can be passed without a value. For example “http://services.neilsbohrlibrary.com/books?redirecturl”. More information can be found here.

Clone this wiki locally