【问题标题】:Rocket not parsing RawStr in a URL to match routeRocket 未在 URL 中解析 RawStr 以匹配路由
【发布时间】:2018-03-11 10:37:37
【问题描述】:

guide on Rocket's site 表示可以对动态路由进行排名。该示例使用不同类型的动态部分作为路由的匹配条件。当我输入一个包含 usize 以外的任何内容的 url 时,我收到以下错误:

GET /user/three text/html:
    => Matched: GET /user/<id>
    => Failed to parse 'id': RawStr("three")
    => Outcome: Forward
    => Error: No matching routes for GET /user/three text/html.
    => Warning: Responding with 404 Not Found catcher.
    => Response succeeded.

我正在使用的代码:

#![feature(plugin)]
#![plugin(rocket_codegen)]

extern crate rocket;
use rocket::http::RawStr;

#[get("/user/<id>")]
fn user(id: usize) -> String { format!("First rank") }

#[get("/user/<id>", rank = 2)]
fn user_int(id: isize) -> String { format!("second rank") }

#[get("/user/<id>", rank = 3)]
fn user_str(id: &RawStr) -> String { format!("last rank") }

fn main() {
    rocket::ignite().mount("/", routes![user]).launch();
}

我希望/user/three 的页面显示测试last rank 而不是404 error。为什么不这样做?

【问题讨论】:

    标签: rust rust-rocket


    【解决方案1】:

    Rocket 不知道你的路线除非你告诉它

    fn main() {
        rocket::ignite().mount("/", routes![user, user_int, user_str]).launch();
        //                                      ^^^^^^^^^^^^^^^^^^^^
    }
    

    【讨论】:

    • 是的,这一秒才发现。
    • 有关参考,请参阅 Rocket 指南中的 Mounting sectionmount 方法作为输入:...通过 routes! 宏的路线列表。
    猜你喜欢
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 2015-02-02
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多