Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Controllers routing

Alexanderius edited this page Jun 25, 2019 · 7 revisions

Controllers routing

In the Get, Post, Put, Patch and Delete controller attributes you can specify a way how a controller handles request path.

If a current request path is matched controller's path and request method (GET, POST etc.) only then the controller will be executed, otherwise the controller with the Http404 attribute will be executed or HTTP 404 status will be returned to a client (if there is not controller with Http404 attribute).

Basic routing

Multiple routes example

    // Controller will process both HTTP GET and POST request with "/foo" path
    [Get("foo")]
    [Post("foo")]
    public class FooController : Controller
    {
        public override ControllerResponse Invoke()
        {
            return StaticTpl("Foo");
        }
    }

Controller dynamic parts

In the controller routing you can specify a dynamic parts which will be parsed and converted to a variable, which then can be accessed via the controller property RouteParameters:

"/foo/{varname}" or "/foo/{varname:vartype}"

varname - variable name, vartype variable type.

Example

    [Get("/user/show/{userName}")]
    public class EditUserController : Controller
    {
        public override ControllerResponse Invoke()
        {
            // Getting parsed user name
            var currentUserName = RouteParameters.userName;
            ...
        }
    }

On request like "http://mywebsite.com/user/show/foouser", currentUserName variable will contain foouser.

Specifying variable type

    [Get("/user/edit/{id:int}")]
    public class EditUserController : Controller
    {
        public override ControllerResponse Invoke()
        {
            var id = RouteParameters.id;
            ...
        }
    }

On request like "http://mywebsite.com/user/edit/15", id variable will contain 15 and will be of an integer type.

Current supported variables types

  • string
  • int
  • decimal
  • bool
  • string[]
  • int[]
  • decimal[]
  • bool[]

If the variables type is not specified, then it will be parsed as a string.

If the variable type is not matched with an actual value, for example, controller path is "/user/edit/{id:int}", actual URL is "http://mywebsite.com/user/edit/foo", then a controller with Http404 attribute will be executed (or HTTP 404 error returned to a client).

You can specify multiple dynamic parts in any ways:

  • "/foo/{myvar}/bar/{myvar2:int}"
  • "/foo/bar/{myvar}/{myvar2:int}/{myvar3}"

<< Previous page Next page >>

Clone this wiki locally