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

时间:2013-08-16 17:57:43

标签: asp.net-mvc-routing

我认为这与How to disable binding of the route values in 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绑定到代理操作,因此path参数最终得到值some/path/here而不是some/path/here?x=1234。如何防止此行为并将查询参数包含在路径参数中?

1 个答案:

答案 0 :(得分:0)

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

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

相关问题