Function actix_web::web::scope

source ·
pub fn scope(path: &str) -> Scope
Expand description

Creates scope for common path prefix.

Scopes collect multiple paths under a common path prefix. The scope’s path can contain dynamic path segments.

Avoid Trailing Slashes

Avoid using trailing slashes in the scope prefix (e.g., web::scope("/scope/")). It will almost certainly not have the expected behavior. See the documentation on resource definitions to understand why this is the case and how to correctly construct scope/prefix definitions.

Examples

In this example, three routes are set up (and will handle any method):

  • /{project_id}/path1
  • /{project_id}/path2
  • /{project_id}/path3

Examples

use actix_web::{web, App, HttpResponse};

let app = App::new().service(
    web::scope("/{project_id}")
        .service(web::resource("/path1").to(|| HttpResponse::Ok()))
        .service(web::resource("/path2").to(|| HttpResponse::Ok()))
        .service(web::resource("/path3").to(|| HttpResponse::MethodNotAllowed()))
);