pub struct Path<T> { /* private fields */ }Expand description
Extract typed information from the request’s path.
If the path segments cannot be deserialized into T, extraction fails with
PathError, which the default error domain
renders as 404 Not Found.
Path segments are percent-decoded after the path is split on /, so
extracted values can contain any character, including /. For example
/files/..%2F..%2Fetc matches /files/{name} with name set to
../../etc. Validate such values before using them, e.g. as a file system
path.
§Example
use ntex::web;
/// extract path info from "/{username}/{count}/index.html" url
/// {username} - deserializes to a String
/// {count} - deserializes to a u32
async fn index(info: web::types::Path<(String, u32)>) -> String {
format!("Welcome {}! {}", info.0, info.1)
}
fn main() {
let app = web::App::default().service(
web::resource("/{username}/{count}/index.html") // <- define path parameters
.route(web::get().to(index)) // <- register handler with `Path` extractor
);
}It is possible to extract path information to a specific type that
implements Deserialize trait from serde.
use std::convert::Infallible;
use ntex::web;
#[derive(serde::Deserialize)]
struct Info {
username: String,
}
/// extract `Info` from a path using serde
async fn index(info: web::types::Path<Info>) -> Result<String, Infallible> {
Ok(format!("Welcome {}!", info.username))
}
fn main() {
let app = web::App::default().service(
web::resource("/{username}/index.html") // <- define path parameters
.route(web::get().to(index)) // <- use handler with `Path` extractor
);
}Implementations§
Source§impl<T> Path<T>
impl<T> Path<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Deconstruct to an inner value
Trait Implementations§
impl<T: Eq> Eq for Path<T>
Source§impl<T, St: State> FromRequest<St> for Path<T>where
T: DeserializeOwned,
Extract typed information from the request’s path.
impl<T, St: State> FromRequest<St> for Path<T>where
T: DeserializeOwned,
Extract typed information from the request’s path.
§Example
use ntex::web;
/// extract path info from "/{username}/{count}/index.html" url
/// {username} - deserializes to a String
/// {count} - deserializes to a u32
async fn index(info: web::types::Path<(String, u32)>) -> String {
format!("Welcome {}! {}", info.0, info.1)
}
fn main() {
let app = web::App::default().service(
web::resource("/{username}/{count}/index.html") // <- define path parameters
.route(web::get().to(index)) // <- register handler with `Path` extractor
);
}It is possible to extract path information to a specific type that
implements Deserialize trait from serde.
use std::convert::Infallible;
use ntex::web;
#[derive(serde::Deserialize)]
struct Info {
username: String,
}
/// extract `Info` from a path using serde
async fn index(info: web::types::Path<Info>) -> Result<String, Infallible> {
Ok(format!("Welcome {}!", info.username))
}
fn main() {
let app = web::App::default().service(
web::resource("/{username}/index.html") // <- define path parameters
.route(web::get().to(index)) // <- use handler with `Path` extractor
);
}Source§impl<T: Ord> Ord for Path<T>
impl<T: Ord> Ord for Path<T>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialOrd> PartialOrd for Path<T>
impl<T: PartialOrd> PartialOrd for Path<T>
impl<T: PartialEq> StructuralPartialEq for Path<T>
Auto Trait Implementations§
impl<T> Freeze for Path<T>where
T: Freeze,
impl<T> RefUnwindSafe for Path<T>where
T: RefUnwindSafe,
impl<T> Send for Path<T>where
T: Send,
impl<T> Sync for Path<T>where
T: Sync,
impl<T> Unpin for Path<T>where
T: Unpin,
impl<T> UnsafeUnpin for Path<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Path<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.