Skip to content

Router Configuration

To configure routing in your application, use the following code snippet. The Configure method accepts a lambda function where you can set various properties on the RouterConfig object to control the behavior of routing and view handling in AventusSharp.

RouterMiddleware.Configure((config) =>
{
// Set configuration properties here
// Example:
// config.PrintRoute = true;
});

The RouterConfig class provides several configuration options for managing routes, views, JSON serialization, and file uploads. Below is a detailed description of each configuration option available.

Properties

ViewDir

  • Type: Func<HttpContext, IRouter?, string>
  • Description: Specifies the directory for views based on the current HTTP context and router instance. By default, it returns the Views directory in the current working directory.

FileUploadTempDir

  • Type: string
  • Default: Path to a temp folder in the application’s root directory.
  • Description: Defines the directory for storing temporary files during uploads. The default path is {AppDomain.CurrentDomain.BaseDirectory}/temp, but you can set it to any location based on your requirements.

transformPattern

  • Type: Func<string, Dictionary<string, RouterParameterInfo>, Type, MethodInfo, Regex>?
  • Description: A function that generates a Regex pattern to match routes dynamically based on route parameters, method information, and other attributes. This can be used to create advanced routing patterns that adapt based on context.

JSONSettings

  • Type: JsonSerializerSettings
  • Description: Configures JSON serialization settings for the router, including handling of null values, date formats, and custom converters. By default, it uses:
    • TypeNameHandling.Auto to include type metadata.
    • NullValueHandling.Ignore to omit null values in JSON.
    • DateFormatHandling.IsoDateFormat with the date format "yyyy-MM-ddTHH:mm:ss.fffZ" to ensure consistency.
    • A custom converter, AventusJsonConverter, for additional serialization control.

PrintRoute

  • Type: bool
  • Default: false
  • Description: If set to true, all routes will be listed in the console at application startup. Useful for debugging and verifying that all routes are correctly registered.

PrintTrigger

  • Type: bool
  • Default: false
  • Description: Enables logging of each route that is triggered, displaying them in the console when accessed. This can assist in tracking and debugging route usage in real-time.

Default Configuration

By default, the RouterConfig class sets the ViewDir property to locate views in the Views folder within the current working directory. This default setup allows you to organize your view files in a dedicated directory without additional configuration. However, each setting can be customized to fit your specific routing and view requirements.

Example: Custom Router Configuration

To set up a custom configuration for your router, such as changing the view directory and enabling route logging, use the following code:

RouterMiddleware.Configure((config) =>
{
config.ViewDir = (context, router) =>
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CustomViews");
};
config.PrintRoute = true;
config.PrintTrigger = true;
});

In this example:

  • ViewDir is configured to use a custom directory, CustomViews, instead of the default Views folder.
  • PrintRoute and PrintTrigger are both set to true, enabling logging of all routes at startup and every time a route is triggered.

This configuration provides a straightforward method for setting up routing behavior in AventusSharp, with options to customize view locations, JSON handling, route logging, and more.