私は新しい Web Api MVC4 プロジェクトを開始しましたが、ルーターで何が間違っているのかを理解することができません。単純な Get リクエストのルートが 404 を返しています。
これが私のルート定義です:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("ApiDefault", "api/{controller}/{id}",
new {controller = "Home", id = UrlParameter.Optional});
routes.MapRoute("ApiDefault-CategoryLevels", "api/{controller}/{level1}/{level2}/{level3}/{level4}",
new
{
controller = "Home",
level1 = UrlParameter.Optional,
level2 = UrlParameter.Optional,
level3 = UrlParameter.Optional,
level4 = UrlParameter.Optional
});
routes.MapRoute("Default", "{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional});
}
ApiDefault-CategoryLevels ルートを使用しようとしています。Phil Haack のルーティング デバッガーをインストールしました。この URL を参照すると、次のように表示されます。
http://localhost:22283/api/Categories/a/b/c/d
私が探しているルートが一致していること:
一致したルート: api/{controller}/{level1}/{level2}/{level3}/{level4} ルートデータ キー値 コントローラーのカテゴリー レベル1 レベル2b レベル3 c レベル4日
ただし、IIS Web サーバーからの戻り値は 404 です。
クエリ文字列を使用して、この方法で同じ URL を呼び出すと、次のように動作します。
http://localhost:22283/api/Categories?level1=A&level2=B&level3=C&level4=D
Web リクエストが機能し、期待していた結果が返されました。最初の方法でリクエストを試行した場合にのみ、404 が返されます。
私の CategoriesController の全体は次のとおりです。
public class CategoriesController : ApiController
{
public IEnumerable<CategoryModel> Get(string level1 = null, string level2 = null, string level3 = null, string level4 = null)
{
return new[] {new CategoryModel()};
}
public CategoryModel Get(Guid id)
{
return new CategoryModel();
}
}