Phema.Routing 3.0.4

C# typed routing library for ASP.NET Core

There is a newer version of this package available.
See the version list below for details.
Install-Package Phema.Routing -Version 3.0.4
dotnet add package Phema.Routing --version 3.0.4
<PackageReference Include="Phema.Routing" Version="3.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Phema.Routing --version 3.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Phema.Routing, 3.0.4"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Phema.Routing as a Cake Addin
#addin nuget:?package=Phema.Routing&version=3.0.4

// Install Phema.Routing as a Cake Tool
#tool nuget:?package=Phema.Routing&version=3.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Phema.Routing

Build Status
Nuget
Nuget

C# strongly typed routing library for ASP.NET Core built on top of MVC Model conventions with built-in From.*, ApiExplorer, Authorization and Caching support

Features

  • Strongly typed url generation
  • BindingSources selection e.g. From.Body, From.Route, From.From
  • Named parameters override e.g. From.Query<int>("query"), From.Route<string>("route") /{route}?query=12345
  • Flexible constraint and filter configuration
  • Extension methods for authorization, caching, api documentation, etc

Usage

// Startup
services.AddControllers() // `.AddMvc()`, `.AddMvcCore()`
  .AddRouting(routing =>
  {
    // Simple route
    routing.AddController<OrdersController>("orders", controller =>
      controller.AddRoute("dashboard", c => c.Dashboard())
        .HttpGet());

    // Authorize controller with "id" parameter from query
    routing.AddController<TaskController>("tasks", controller =>
      controller.AddRoute("edit/{id}", c => c.Edit(From.Route<int>("id"))) // `From.*` is matches `[From*]` attributes
        .HttpGet()
        .Authorize());

    // Multiple routes in one controller
    routing.AddController<WashingController>("washing", controller =>
    {
      // Override controller `.Authorize()`
      controller.AddRoute("wash", c => c.Wash(From.Query<int>())) // If name not specified - used method parameter name
        .HttpGet()
        .AllowAnonymous()

      // Inherit authorization requirement from controller
      controller.AddRoute("dry", c => c.Dry(From.Body<DryModel>()))
        .HttpPost()
        .ValidateAntiForgeryToken();
    }).Authorize();

    // Multiple binding sources
    routing.AddController<MoviesController>("movies", controller =>
      controller.AddRoute("{category}/upload", c =>
        c.Upload(From.Route<string>(), From.Body<UploadMovieModel>(), From.Query<bool>("compress")))
        .HttpPost());
  });

Action url generation (IUrlHelper used)

// From.Query with parameter override
services.AddRouting(routing =>
  routing.AddController<Controller>("controller", controller =>
    controller.AddRoute("action", c => c.Action(From.Query<int>("parameter")))));

// "controller/action?parameter=10"
var action = Url.Action<Controller>(c => c.Action(10));

// From.Route with route part override
services.AddRouting(routing =>
  routing.AddController<Controller>("controller", controller =>
    controller.AddRoute("action/{mode}", c => c.Route(From.Route<Mode>("mode")))));

// "controller/action/access"
var action = Url.Action<Controller>(c => c.Action("access"));

// Combine query and route parts
services.AddRouting(routing =>
  routing.AddController<Controller>("controller", controller =>
    controller.AddRoute("action/{mode}", c => c.Route(From.Query<int>(), From.Route<Mode>()))));

// "controller/action/access?parameter=10"
var action = Url.Action<Controller>(c => c.Action(10, "access"));

// Get values from current request using From.Query in IUrlHelper generation
services.AddRouting(routing =>
  routing.AddController<Controller>("controller", controller =>
    controller.AddRoute("action", c => c.Action(From.Query<int>()))));

// P.S. BindingSources not used in url will ignored (e.g. `From.Body<TModel>()` used only for compile reasons)
// "controller/action?parameter=VALUE_FROM_CURRENT_REQUEST"
var action = Url.Action<Controller>(c => c.Action(From.Query<int>()));

Extensions

  • Http methods - .HttpPost(), .HttpGet()...
  • Caching - .Cached(...)
  • Authorization - .Authorize(...), .AllowAnonymous()
  • AntiForgeryToken - .ValidateAntiForgeryToken(), .IgnoreAntiforgeryToken()
  • Https - .RequireHttps()
  • RequestSizeLimit - .RequestSizeLimit(...), .DisableRequestSizeLimit()
  • Produces, Consumes for ApiExplorer - .Produces<TModel>(...), .Consumes(...)

Phema.Routing

Build Status
Nuget
Nuget

C# strongly typed routing library for ASP.NET Core built on top of MVC Model conventions with built-in From.*, ApiExplorer, Authorization and Caching support

Features

  • Strongly typed url generation
  • BindingSources selection e.g. From.Body, From.Route, From.From
  • Named parameters override e.g. From.Query<int>("query"), From.Route<string>("route") /{route}?query=12345
  • Flexible constraint and filter configuration
  • Extension methods for authorization, caching, api documentation, etc

Usage

// Startup
services.AddControllers() // `.AddMvc()`, `.AddMvcCore()`
  .AddRouting(routing =>
  {
    // Simple route
    routing.AddController<OrdersController>("orders", controller =>
      controller.AddRoute("dashboard", c => c.Dashboard())
        .HttpGet());

    // Authorize controller with "id" parameter from query
    routing.AddController<TaskController>("tasks", controller =>
      controller.AddRoute("edit/{id}", c => c.Edit(From.Route<int>("id"))) // `From.*` is matches `[From*]` attributes
        .HttpGet()
        .Authorize());

    // Multiple routes in one controller
    routing.AddController<WashingController>("washing", controller =>
    {
      // Override controller `.Authorize()`
      controller.AddRoute("wash", c => c.Wash(From.Query<int>())) // If name not specified - used method parameter name
        .HttpGet()
        .AllowAnonymous()

      // Inherit authorization requirement from controller
      controller.AddRoute("dry", c => c.Dry(From.Body<DryModel>()))
        .HttpPost()
        .ValidateAntiForgeryToken();
    }).Authorize();

    // Multiple binding sources
    routing.AddController<MoviesController>("movies", controller =>
      controller.AddRoute("{category}/upload", c =>
        c.Upload(From.Route<string>(), From.Body<UploadMovieModel>(), From.Query<bool>("compress")))
        .HttpPost());
  });

Action url generation (IUrlHelper used)

// From.Query with parameter override
services.AddRouting(routing =>
  routing.AddController<Controller>("controller", controller =>
    controller.AddRoute("action", c => c.Action(From.Query<int>("parameter")))));

// "controller/action?parameter=10"
var action = Url.Action<Controller>(c => c.Action(10));

// From.Route with route part override
services.AddRouting(routing =>
  routing.AddController<Controller>("controller", controller =>
    controller.AddRoute("action/{mode}", c => c.Route(From.Route<Mode>("mode")))));

// "controller/action/access"
var action = Url.Action<Controller>(c => c.Action("access"));

// Combine query and route parts
services.AddRouting(routing =>
  routing.AddController<Controller>("controller", controller =>
    controller.AddRoute("action/{mode}", c => c.Route(From.Query<int>(), From.Route<Mode>()))));

// "controller/action/access?parameter=10"
var action = Url.Action<Controller>(c => c.Action(10, "access"));

// Get values from current request using From.Query in IUrlHelper generation
services.AddRouting(routing =>
  routing.AddController<Controller>("controller", controller =>
    controller.AddRoute("action", c => c.Action(From.Query<int>()))));

// P.S. BindingSources not used in url will ignored (e.g. `From.Body<TModel>()` used only for compile reasons)
// "controller/action?parameter=VALUE_FROM_CURRENT_REQUEST"
var action = Url.Action<Controller>(c => c.Action(From.Query<int>()));

Extensions

  • Http methods - .HttpPost(), .HttpGet()...
  • Caching - .Cached(...)
  • Authorization - .Authorize(...), .AllowAnonymous()
  • AntiForgeryToken - .ValidateAntiForgeryToken(), .IgnoreAntiforgeryToken()
  • Https - .RequireHttps()
  • RequestSizeLimit - .RequestSizeLimit(...), .DisableRequestSizeLimit()
  • Produces, Consumes for ApiExplorer - .Produces<TModel>(...), .Consumes(...)

  • .NETCoreApp 3.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
3.0.5 314 9/25/2019
3.0.4 240 8/27/2019
3.0.3 250 7/27/2019
3.0.2 224 7/26/2019
3.0.1 240 7/25/2019
3.0.0 227 7/19/2019