Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying ServiceStack 4 Cookbook
  • Table Of Contents Toc
  • Feedback & Rating feedback
ServiceStack 4 Cookbook

ServiceStack 4 Cookbook

By : Hodgson
4.7 (3)
close
close
ServiceStack 4 Cookbook

ServiceStack 4 Cookbook

4.7 (3)
By: Hodgson

Overview of this book

If you are a .NET developer who is looking for a simpler way to build services, this is the book for you. It will show you how to write fast, maintainable APIs that are a pleasure to use and maintain starting from the database to the client and everything in-between.
Table of Contents (13 chapters)
close
close
12
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.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
ServiceStack 4 Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon