0

Short and simple. I have a route like this:

config.Routes.MapHttpRoute(
                name: "QuickSearch",
                routeTemplate: "QuickSearch/{searchQuery}",
                defaults: new { controller = "QuickSearch", action = "Search" },
                constraints: new { httpMethod = new HttpMethodConstraint(new string[] { "GET" }), }
                );

And an action that looks like this:

public class QuickSearchController : ApiController
    {
        [HttpGet]
        public QuickSearchResponse Search(string searchQuery)
        {
        }
    }

If I call http://localhost:4096/QuickSearch/SearchMe, then "SearchMe" gets bound to the searchQuery parameter, and the Search method is invoked successfully.

If, however, I call http://localhost:4096/QuickSearch/?searchQuery=SearchMe then I receive a 404 message saying that the "Requested URL: /QuickSearch" cannot be found.

I thought the above URLs would be equivilent? Why doesn't it work when I use a query string?

4

1 回答 1

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" }), }
    );
于 2013-08-28T10:09:47.497 回答