2013-04-28 8 views
6

WebAPIが使用するデフォルトRouteConfigurationの代わりにAttributeRoutingを使用するときに、デフォルトコントローラを使用する方法を設定します。私は上記のセクションをコメントする場合AttributeRoutingを使用してWebAPIのデフォルトコントローラ/アクションルートを指定します。

public class RouteConfig 
    { 
     public static void RegisterRoutes(RouteCollection routes) 
     { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
     //routes.MapRoute(
     // name: "Default", 
     // url: "{controller}/{action}/{id}", 
     // defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     //); 

     } 
    } 

をAttribteRouting使用してWEBAPIアプリを実行しようとすると、これは冗長であるため、 すなわちコメントコードセクションを取り除く、デフォルトのホームコントローラが存在しないので、私は次のエラーを取得します/アクション定義。 HTTPエラー403.14 - 禁止 Webサーバーは、このディレクトリの内容を一覧表示しないように構成されています。

ホームコントローラ/アクションの属性ルーティングを使用してルートを指定するにはどうすればよいですか?

EDIT:コードサンプル:

//[RoutePrefix("")] 
public class HomeController : Controller 
{ 
    [GET("")] 
    public ActionResult Index() 
    { 
     return View(); 
    } 
} 

これは「」としてURLテンプレートをコレクションにルートを追加し、デフォルトにコントローラとアクションのために:

public class HomeController : Controller 
{ 
    [GET("")] 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    public ActionResult Help() 
    { 
     var explorer = GlobalConfiguration.Configuration.Services.GetApiExplorer(); 
     return View(new ApiModel(explorer)); 
    } 
} 
+0

あなたはAttributeRoutingを使用していると書いているので、AttributeRouting属性でマークされたコントローラーの動作を示すコードサンプルを提供してくださいか? –

+0

@Pete:投稿を更新しましたコードサンプルを含める –

答えて

5

は、次を試してみました「ホーム」と「インデックス」となります。

+0

上記は機能していないようです。コードスニペットを含めるように投稿を更新しました –

+0

属性ルーティングのエクステンションを呼び出すかどうかを確認することもできますion'MapAttributeRoutes'? –

+0

AttributeRoutingHttp静的クラスのRegisterRoutesメソッドではあります。 –

1

すでにインストールされているAttributeRouting (ASP.NET Web API)の横にAttributeRouting (ASP.NET MVC)をインストールする必要があります。

MVCおよびWeb APIパッケージは、無料パッケージです。両方ともAttributeRouting.Core。*パッケージに依存します。 MVCのARはControllerメソッドのルートです。 Web APIのARは、ApiControllerメソッドのルート用です。

+1

WebAPIのAttributeRoutingと一緒にMVCのAttributeRoutingをインストールすることをお勧めしますか? –

+1

はい、MVCパッケージを追加する必要があります。これらは両方ともAttributeRouting.Core。*パッケージに依存する無料パッケージです。 MVCのARはControllerメソッドのルートです。 Web APIのARは、ApiControllerメソッドのルート用です。 (私はそれを明確にするために私の答えを編集した) –

1

これは私のために働いた。

using System.Web.Http; 

namespace MarwakoService 
{ 
     public static class WebApiConfig 
     { 
      public static void Register(HttpConfiguration config) 
      { 
       // Web API routes 
       config.MapHttpAttributeRoutes(); 

       // Other Web API configuration not shown. 
      } 
     } 
} 

あなたが慣例に基づく属性を組み合わせることができます。次のコードを入れてApp_StartフォルダにあるあなたのWebApiConfigクラスのルーティング属性を有効にしWebApiConfigクラス

config.Routes.MapHttpRoute(
       name: "AppLaunch", 
       routeTemplate: "", 
       defaults: new 
       { 
        controller = "Home", 
        action = "Get" 
       } 
      ); 
1

Register()にこれを追加します。:今すぐあなたのGlobal.asaxのクラスに移動し、次のコード行を追加します

public static class WebApiConfig 
{ 
    public static void Register(HttpConfiguration config) 
    { 
      // Attribute routing. 
      config.MapHttpAttributeRoutes(); 

      // Convention-based routing. 
      config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 
    } 
} 
GlobalConfiguration.Configure(WebApiConfig.Register); 

あなたのHomeControllerはMVCコントローラです。

[RoutePrefix("api/Product")] 
public class CustomersController : ApiController 
{ 
    [Route("{customerName}/{customerID:int}/GetCreditCustomer")] 
    public IEnumerable<uspSelectCreditCustomer> GetCreditCustomer(string customerName, int customerID) 
    { 
     ... 
    } 

あなたは両方のMVCをチェックすると、標準のMVCアプリケーションはMVCとWEBAPIの両方をサポートしていることができ、それは

0

を役に立てば幸いと:(さんはCustomersControllerを言わせて)、次のようにあなたのルートは属性を追加APIコントローラを作成してみてくださいCreate New Asp.net WebアプリケーションテンプレートのWebapi。 「ルートと

public class RouteConfig 
{ 
    public static void RegisterRoutes(RouteCollection routes) 
    { 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    routes.MapMvcAttributeRoutes(); 
    routes.MapRoute(
     name: "Default", 
     url: "{controller}/{action}/{id}", 
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); 

    } 
} 

お知らせライン:あなたはあなたのクエリがMVCのルートではなくWEBAPIものに特定されるようRouteConfig.csの下に次の操作を行う必要があります

。"routes.MapRoute(...)"の呼び出しで属性のルートを提供するMapMvcAttributeRoutes();は、デフォルトのコントローラとアクションへの従来のルートを提供します。

属性ルーティングと従来のルーティングでは、 希望すること

関連する問題