Struct ntex::web::Route [−][src]
pub struct Route<Err: ErrorRenderer = DefaultError> { /* fields omitted */ }
Expand description
Resource route definition
Route uses builder-like pattern for configuration. If handler is not explicitly set, default 404 Not Found handler is used.
Implementations
Add method guard to the route.
App::new().service(web::resource("/path").route(
web::route()
.method(http::Method::CONNECT)
.guard(guard::Header("content-type", "text/plain"))
.to(|req: HttpRequest| async { HttpResponse::Ok() }))
);
Add guard to the route.
App::new().service(web::resource("/path").route(
web::route()
.guard(guard::Get())
.guard(guard::Header("content-type", "text/plain"))
.to(|req: HttpRequest| async { HttpResponse::Ok() }))
);
Set handler function, use request extractors for parameters.
use ntex::web;
#[derive(serde::Deserialize)]
struct Info {
username: String,
}
/// extract path info using serde
async fn index(info: web::types::Path<Info>) -> String {
format!("Welcome {}!", info.username)
}
fn main() {
let app = web::App::new().service(
web::resource("/{username}/index.html") // <- define path parameters
.route(web::get().to(index)) // <- register handler
);
}
It is possible to use multiple extractors for one handler function.
use ntex::web;
#[derive(serde::Deserialize)]
struct Info {
username: String,
}
/// extract path info using serde
async fn index(path: web::types::Path<Info>, query: web::types::Query<HashMap<String, String>>, body: web::types::Json<Info>) -> String {
format!("Welcome {}!", path.username)
}
fn main() {
let app = web::App::new().service(
web::resource("/{username}/index.html") // <- define path parameters
.route(web::get().to(index))
);
}
Trait Implementations
type Response = WebResponse
type Response = WebResponse
Responses given by the service
type Service = RouteService<Err>
type Service = RouteService<Err>
The Service
value created by this factory
Create and return a new service value asynchronously.
Map this service’s output to a different type, returning a new service of the resulting type. Read more
Map this service’s error to a different error, returning a new service.
fn map_init_err<F, E>(self, f: F) -> MapInitErr<Self, Req, Cfg, F, E> where
F: Fn(Self::InitError) -> E + Clone,
fn map_init_err<F, E>(self, f: F) -> MapInitErr<Self, Req, Cfg, F, E> where
F: Fn(Self::InitError) -> E + Clone,
Map this factory’s init error to a different error, returning a new service.
Auto Trait Implementations
impl<Err = DefaultError> !RefUnwindSafe for Route<Err>
impl<Err = DefaultError> !Send for Route<Err>
impl<Err = DefaultError> !Sync for Route<Err>
impl<Err = DefaultError> !UnwindSafe for Route<Err>
Blanket Implementations
Mutably borrows from an owned value. Read more
Convert Self
to a ServiceFactory
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more