简短而简单。我有这样的路线:
config.Routes.MapHttpRoute(
name: "QuickSearch",
routeTemplate: "QuickSearch/{searchQuery}",
defaults: new { controller = "QuickSearch", action = "Search" },
constraints: new { httpMethod = new HttpMethodConstraint(new string[] { "GET" }), }
);
一个看起来像这样的动作:
public class QuickSearchController : ApiController
{
[HttpGet]
public QuickSearchResponse Search(string searchQuery)
{
}
}
如果我调用http://localhost:4096/QuickSearch/SearchMe
,则“SearchMe”会绑定到searchQuery参数,并且会成功调用Search方法。
但是,如果我拨打http://localhost:4096/QuickSearch/?searchQuery=SearchMe
,则会收到404消息,指出无法找到“请求的网址:/快速搜索”。
我认为以上网址是等效的吗?当我使用查询字符串时,为什么它不起作用?
答案 0 :(得分:0)
如果您将searchQuery添加为可选参数,您也应该能够使用查询字符串来执行控制器操作:
config.Routes.MapHttpRoute(
name: "QuickSearch",
routeTemplate: "QuickSearch/{searchQuery}",
defaults: new { controller = "QuickSearch", action = "Search", searchQuery = RouteParameter.Optional },
constraints: new { httpMethod = new HttpMethodConstraint(new string[] { "GET" }), }
);