[][src]Struct ntex::web::Route

pub struct Route<Err: ErrorRenderer = DefaultError> { /* fields omitted */ }

Resource route definition

Route uses builder-like pattern for configuration. If handler is not explicitly set, default 404 Not Found handler is used.

Implementations

impl<Err: ErrorRenderer> Route<Err>[src]

pub fn new() -> Route<Err>[src]

Create new route which matches any request.

impl<Err: ErrorRenderer> Route<Err>[src]

pub fn method(mut self: Self, method: Method) -> Self[src]

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() }))
);

pub fn guard<F: Guard + 'static>(mut self: Self, f: F) -> Self[src]

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() }))
);

pub fn to<F, Args>(mut self: Self, handler: F) -> Self where
    F: Handler<Args, Err>,
    Args: FromRequest<Err> + 'static,
    Args::Error: Into<Err::Container>,
    <F::Output as Responder<Err>>::Error: Into<Err::Container>, 
[src]

Set handler function, use request extractors for parameters.

use ntex::web;
use serde_derive::Deserialize;

#[derive(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(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

impl<Err: ErrorRenderer> IntoRoutes<Err> for Route<Err>[src]

impl<Err: ErrorRenderer> ServiceFactory for Route<Err>[src]

type Config = ()

Service factory configuration

type Request = WebRequest<Err>

Requests handled by the service.

type Response = WebResponse

Responses given by the service

type Error = Err::Container

Errors produced by the service

type InitError = ()

Errors produced while building a service.

type Service = RouteService<Err>

The Service value created by this factory

type Future = Ready<Result<RouteService<Err>, ()>>

The future of the ServiceFactory instance.

Auto Trait Implementations

impl<Err = DefaultError> !RefUnwindSafe for Route<Err>[src]

impl<Err = DefaultError> !Send for Route<Err>[src]

impl<Err = DefaultError> !Sync for Route<Err>[src]

impl<Err> Unpin for Route<Err>[src]

impl<Err = DefaultError> !UnwindSafe for Route<Err>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> IntoServiceFactory<T> for T where
    T: ServiceFactory
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,