Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ServiceStack 4 Cookbook

You're reading from   ServiceStack 4 Cookbook Over 70 recipes to create web services, build message-based apps, and work with object-relational mapping

Arrow left icon
Product type Paperback
Published in Jan 2015
Publisher
ISBN-13 9781783986569
Length 444 pages
Edition 1st Edition
Arrow right icon
Toc

Table of Contents (13) Chapters Close

Preface 1. Configuration and Routing FREE CHAPTER 2. Services and Data Transfer Objects 3. Testing and Logging 4. Object Relational Mapping (OrmLite) 5. HTML and Form Data 6. Filters and Validators 7. Security and Authentication 8. Working with Redis 9. Integrating with Other Technologies A. Getting Started B. Testing Locally Index

Adding Routes via the API

This recipe covers the ability to add routes without using the provided RouteAttribute class. This technique might be needed if you have restrictions on your development environment or requirements that might prevent you from using RouteAttribute. In this situation where it's not ideal or possible to use the ServiceStack C# client, a client such as RestSharp with data transfer objects might be possible alternative.

Note

Using the routing attributes gives you the advantages of more streamlined development when using the ServiceStack JsonClient and is the recommended way of managing your application's routes. The solution in this recipe is only intended for situations where this is not possible.

How to do It...

From the AppHost class, access the Routes property on the base ServiceStackHost class to add routes, passing the request object's type and the path to be used:

public class AppHost : AppHostBase
{
  public AppHost(): base("Adding Routes via AppHost", typeof(AppHost).Assembly)
  { }
  
  public override void Configure(Container container)
  {
    Routes.Add<GreeterRequest>("/greetings/{Name}");
    Routes.Add<FarewellRequest>("/farewell/{Name}", "GET");
    Routes.Add<HowAreYouRequest>("/howareyou/{Name}", ApplyTo.Get);
    Routes.Add<IntroRequest>("/introducing/{0}/and/{1}",ApplyTo.Get,request => request.FirstName,request => request.SecondName);
    Routes.Add<IntroRequest>("/introducing/{0}/and/{1}/otherway",ApplyTo.Get, request => request.SecondName, request => request.FirstName);
    Routes.AddFromAssembly(typeof(ImFromRequest).Assembly);

  }
}

How it works…

Using this recipe, routes are registered directly with the ServiceStackHost class using two methods.

  • Routes.Add<Type>(path): This is a single registration of a request type and a path, which will map the URL to the service associated with the request object, in this case GreeterRequest is associated with the URL /greetings/{Name}. The {Name} URL binds the value in the place of the path to the Name property of the request object.
  • Routes.Add<Type>(path,"GET, POST, DELETE"): This is another way to register the route with the same passing of property values, but restricting the request to just the verb methods specified. If the verb isn't specified, it won't be available. If you try to access a route that hasn't been registered with the verb being requested, ServiceStack will respond with a 404 status code and default page advising you that the route was not found.
  • Routes.Add<Type>(path, ApplyTo.Get): This is the same as using a string of verbs to restrict the path, but can be useful when trying to avoid possible bugs from spelling mistakes in the list of verbs.
  • Routes.Add<Type>(pathWithFormater, ApplyTo.Get, propertyExpressions): This is an extension method to use for ordered variables to be used map properties to values within the path. This can be useful if you register different mappings with different verb restrictions or when dealing with complex routes that may have different binding behavior. In the example shown, by adding otherway to the end of the same route, we changed the binding behavior of the same service.
  • Routes.AddFromAssembly(assembly): This is a method that requires the use of RouteAttribute to find request objects with routes within a specified assembly.
  • The add method also provides a way to register types that are only known at runtime with alternate methods such as Routes.Add(myTypeInstance, path). This could be used in conjunction with standard .NET reflection methods such as myObjectInstance.GetType(), which can be used on any object.

There's more...

In versions of ServiceStack greater than 4.0.18.0, the GetRouteAttributes(Type type) method is virtual, allowing it to be overridden. This can allow the ability to source an array of RouteAttribute objects from custom locations or using custom logic.

For example, when the server's AppHost class is starting up, this method could check some custom configuration or even call external web services to work out what routes should be used. To achieve this, simply override the GetRouteAttributes method in a suitable place in your code.

You have been reading a chapter from
ServiceStack 4 Cookbook
Published in: Jan 2015
Publisher:
ISBN-13: 9781783986569
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image