如何阻止ASP.NET MVC绑定查询参数



我认为这类似于如何在ASP.NET MVC中禁用路由值的绑定?,但我不明白这个答案。

我在我的一个控制器中有一个代理操作,看起来像这个

public ActionResult Proxy(string path) {
    // Issue a request to another server for the provided path
}

和类似的路由规则

routes.MapRoute("Proxy", "Proxy/{*path}", new { controller = "Proxy", action = "Proxy" });

这允许我代理请求,如http://www.website.com/Proxy/some/path/here到http://api.someotherwebsite.com/some/path/here这很好用。但是,如果我有查询参数,例如。http://www.website.com/Proxy/some/path/here?x=1234,MVC试图将查询参数x绑定到Proxy操作,因此path参数最终得到的值为some/path/here,而不是some/path/here?x=1234。如何防止这种行为并将查询参数包含在路径参数中?

老实说,在您的情况下,将查询字符串附加回路径参数似乎要容易得多,而不是试图破坏自定义路由。

OtherServerRequest(path + "?" + Request.QueryString);

最新更新