-
Notifications
You must be signed in to change notification settings - Fork 9
Controllers routing
In Get, Post, Put and Delete controller attributes you can specify a way how controller handles request path.
If current request path is matched controller's path and request method (GET, POST etc.) only then controller will be executed, otherwise controller with Http404 attribute will be executed or HTTP 404 will be returned (if there is not controller with Http404 attribute).
-
"/"- controller will process only default page requests, for example, if current URL is: http://mywebsite.com -
"/foo"- controller will process only requests with/foopath, for example: http://mywebsite.com/foo -
"/foo/bar/test/action/hello"- you can define any route, which controller can handle, fo example: http://mywebsite.com/foo/bar/test/action/hello -
"foo"- you can specify paths without slash
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 new StaticTpl("Foo");
}
}In controller routing you can specify dynamic parts which will be parsed and converted to a variable, which then can be accessed via 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'.
[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.
Current supported variables types is:
stringintdecimal
If variables type is not specified, then it will be parsed as string.
If variable type is not matched with actual value, for example: controller path is "/user/edit/{id:int}", actual URL: "http://mywebsite.com/user/edit/foo"
', then a controller with Http404 attribute will be executed (or HTTP 404 error returned).
You can specify multiple dynamic parts in any ways, examples:
"/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
- Language manager
- Redirector
- HTML