-
-
Notifications
You must be signed in to change notification settings - Fork 10
Controller routing
In the Get
, Post
, Put
, Patch
, and Delete
controller attributes you can specify how a controller handles the request path.
If the current request path matches the controller's path and request method (GET, POST, etc.), only then will the controller be executed; otherwise, the controller with the Http404 attribute will be executed or HTTP 404 status will be returned to the client (if there is no controller with Http404 attribute).
-
"/"
- the controller will process only default page requests, for example, if the current URL is: http://mywebsite.com -
"/foo"
- the controller will process only requests with/foo
path, for example: http://mywebsite.com/foo -
"/foo/bar/test/action/hello"
- you can define any route which the controller can handle, for example: http://mywebsite.com/foo/bar/test/action/hello -
"foo"
- you can specify a single path part without a slash (http://mywebsite.com/foo)
// Controller will process both HTTP GET and POST requests with "/foo" path
[Get("foo")]
[Post("foo")]
public class FooController : Controller
{
public override ControllerResponse Invoke()
{
return StaticTpl("Foo");
}
}
In the controller routing you can specify dynamic parts which will be parsed and converted to a variable, which then can be accessed via the controller property RouteParameters
:
"/foo/{var-name}"
or "/foo/{var-name:var-type}"
var-name
- variable name, var-type
- variable type.
[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
.
[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 integer type.
In the controller routing you can specify dynamic parts which will be parsed and converted to a variable, which then can be accessed via the controller property RouteParameters
:
"/foo/{var-name}"
var-name
- variable name
The respective route parameter values can be accessed via method parameters with the same name. The method parameter type will specify the respective route parameter type.
The names of the parameters are case-insensitive.
[Get("/some-request/{foo}/{bar}/{test}")]
public class ExampleController : Controller2
{
public void Invoke(string foo, bool bar, int test)
{
...
}
}
On request like "http://mywebsite.com/some-request/str/true/5", foo
parameter will contain str
, bar
parameter will contain true
, test
parameter will contain 5
.
string
int
decimal
bool
string[]
int[]
decimal[]
bool[]
If the variable type is not specified, then it will be parsed as a string
.
If the variable type does not match the 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 the client).
You can specify multiple dynamic parts in any way:
"/foo/{myvar}/bar/{myvar2:int}"
"/foo/bar/{myvar}/{myvar2:int}/{myvar3}"
- Getting Started
- Main Simplify.Web principles
- Simplify.Web controllers
- Simplify.Web views
- Simplify.Web templates
- Simplify.Web configuration
- Templates variables
- Static content
- Template factory
- Data collector
- String table
- File reader
- Web context
- Environment
- Dynamic environment
- Language manager
- Redirector
- HTML