2016-08-28 4 views
-1

WebApiに関連した多くの質問が呼び出されませんでしたが、すべてのソリューションを試しましたが、私の問題に対して正しい解決策を得ることができません。 私が持っているこのjQueryを使用してコントローラからwebApiを呼び出すことができませんAjax

public class shoppingCart : ApiController 
{ 
    [HttpPost] 
    public string getDetails() 
    { 
     return "HttpPost"; 
    } 
    [HttpGet] 
    public string getDetails1() 
    { 
     return "HttpGet"; 
    } 
    [HttpPut] 
    public string getDetails2() 
    { 
     return "HttpPut"; 

    } 
    [HttpDelete] 
    public string getDetails3() 
    { 
     return "HttpDelete"; 
    } 
} 

私global.asax.csファイルのようなWEBAPIがあるこの

public class MvcApplication : System.Web.HttpApplication 
{ 
    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 

     WebApiConfig.Register(GlobalConfiguration.Configuration); 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 
     AuthConfig.RegisterAuth(); 
    } 
} 

私WebApiConfig.csファイルには、この

public static class WebApiConfig 
{ 
    public static void Register(HttpConfiguration config) 
    { 
     config.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = RouteParameter.Optional } 
     );  
    } 
} 

私routeConfig.csのようなものであるようにファイルは次のようなものです

public class RouteConfig 
{ 
    public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 
} 

私のjQueryのAjaxのスクリプトは、誰かが問題を解決するために私を助けてください私は、Web APIを呼び出そうとするたびに、私はエラー

enter image description here

を取得し、この

$('#btnAjax').click(function (e) { 
     e.preventDefault(); 
     $.ajax({ 
      type: 'POST', 
      url: '/api/shoppingCart/getDetails', 
      success: function (returnData) { 
       alert('success'); 
      }, 
      error: function (xhr, ajaxOptions, thrownError) { 
       alert('error'); 
      } 
     }); 
    }); 

のようなものです。いくつかの他の回答として

: はコメントで述べたように、あなたもWebApiConfig

public static void Register(HttpConfiguration config) 
{ 
      //this line 
      config.MapHttpAttributeRoutes(); 
    .... 
} 

編集内の次の行を追加する必要があります。この

[HttpPost] 
[Route("/api/shoppingCart/getDetails")] 
public string getDetails() 
{ 
    return "HttpPost"; 
} 

のように試してみてください、あなたに

+0

あなたのコントローラの名前を変更して、コントローラ

のための命名規則は、次のされていません。私はすべての答えを探していて、あなたが提供したリンクの詰め物は、本当に検索友好的ではなかったと私はあなたの与えられたリンクで提案されている答えを読むことができなかった理由だ –

答えて

2

コントローラの名前をshoppingCartControllerに変更してください。私の知る限り、ASP.NETは要求を見てから、要求の名前が+ Controllerのコントローラを探します。私はいつもコントローラを追加して安全なところにいるようにしています。

+1

ありがとう、名前をshoppingCartControllerに変更して、私の問題を解決しました –

0

ありがとうございましたあなたのコントローラのための命名規則にも従うべきです

+1

あなたはまた、属性ルーティング。現在OPのWebApiConfigは属性ルーティングを許可していません – Nkosi

+0

@Nkosi、私は私の返信を更新しました。 – Robert

+0

通常のルーティングを使用してただ完了できる属性ルーティングを導入することは、不要な(メンテナンス)オーバーヘッドを追加することです。 – CodeCaster

0

あなたはネガティブマークメイトで私の質問をマークする必要はありません@CodeCaster

public class ShoppingCartController : ApiController { ... } 
関連する問題