1Add a fallback service to the router.
2
3This service will be called if no routes matches the incoming request.
4
5```rust
6use axum::{
7    Router,
8    routing::get,
9    handler::Handler,
10    response::IntoResponse,
11    http::{StatusCode, Method, Uri},
12};
13
14let handler = get(|| async {}).fallback(fallback);
15
16let app = Router::new().route("/", handler);
17
18async fn fallback(method: Method, uri: Uri) -> (StatusCode, String) {
19    (StatusCode::NOT_FOUND, format!("`{}` not allowed for {}", method, uri))
20}
21# async {
22# hyper::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
23# };
24```
25
26## When used with `MethodRouter::merge`
27
28Two routers that both have a fallback cannot be merged. Doing so results in a
29panic:
30
31```rust,should_panic
32use axum::{
33    routing::{get, post},
34    handler::Handler,
35    response::IntoResponse,
36    http::{StatusCode, Uri},
37};
38
39let one = get(|| async {}).fallback(fallback_one);
40
41let two = post(|| async {}).fallback(fallback_two);
42
43let method_route = one.merge(two);
44
45async fn fallback_one() -> impl IntoResponse { /* ... */ }
46async fn fallback_two() -> impl IntoResponse { /* ... */ }
47# let app = axum::Router::new().route("/", method_route);
48# async {
49# hyper::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
50# };
51```
52
53## Setting the `Allow` header
54
55By default `MethodRouter` will set the `Allow` header when returning `405 Method
56Not Allowed`. This is also done when the fallback is used unless the response
57generated by the fallback already sets the `Allow` header.
58
59This means if you use `fallback` to accept additional methods, you should make
60sure you set the `Allow` header correctly.
61