2017-09-26 8 views
0

私はAngular 2を学び、ASP MVCセットアップで実行しようとしています。私はこの非常に優れたガイドに従っています:https://www.codeproject.com/Articles/1181888/Angular-in-ASP-NET-MVC-Web-API-PartASP MVC Web APIエンドポイントが返される404

少し修正しました。オリジナルはAPIエンドポイントapi/userapiを介してデータベースから得られた名前のリストを操作することになっています。私はそれを変更して、エンドポイントapi/postapiからの投稿のリスト(ブログなど)になります。プロジェクトの角度部分はこれまでのところ動作しています。それは正しく動作していないASP.NETの部分です。

元のプロジェクト(私はそのリンクからダウンロードし、Visual Studio 2017で正常にテスト済み)の構成を見て、それを私の実験と比較すると、私の人生の間、どこが誤って構成されているのか分かりませんそれ。

ここで問題が発生する可能性がありますか?ここでのWeb APIの一部のための私のコードは次のとおりです。

Global.asax.cs

using System.Web.Http; 
using System.Web.Mvc; 
using System.Web.Optimization; 
using System.Web.Routing; 

namespace WebAPI 
{ 
    public class WebApiApplication : System.Web.HttpApplication 
    { 
     protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 
      GlobalConfiguration.Configure(WebApiConfig.Register); 
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 
      BundleConfig.RegisterBundles(BundleTable.Bundles); 
     } 
    } 
} 

RouteConfig.cs

using System.Web.Mvc; 
using System.Web.Routing; 

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

      routes.MapRoute(
       name: "Default", 
       url: "{*anything}", // I am suspicious of this line but changing it doesn't fix it 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 
     } 
    } 

WebApiConfig.cs

using System.Net.Http.Headers; 
using System.Web.Http; 

namespace WebAPI 
{ 
    public static class WebApiConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 
      // Web API configuration and services 

      // Output as JSON instead of XML 
      config.Formatters.JsonFormatter.SupportedMediaTypes 
       .Add(new MediaTypeHeaderValue("application/octet-stream")); 

      // Web API routes 
      config.MapHttpAttributeRoutes(); 

      config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 
     } 
    } 
} 

PostsAPIController.cs

using WebAPI.Models; 
using System.Data.Entity; 
using System.Linq; 
using System.Net.Http; 
using System.Web.Http; 

namespace WebAPI.Controllers 
{ 
    [RoutePrefix("api/postapi")] //this did not help either 
    public class PostsAPIController : BaseAPIController 
    { 
     public HttpResponseMessage Get() 
     { 
      return ToJson(_db.TWebSitePostsSet.AsEnumerable()); 
     } 

     public HttpResponseMessage Post([FromBody]TWebSitePosts value) 
     { 
      _db.TWebSitePostsSet.Add(value); 
      return ToJson(_db.SaveChanges()); 
     } 

     public HttpResponseMessage Put(int id, [FromBody]TWebSitePosts value) 
     { 
      _db.Entry(value).State = EntityState.Modified; 
      return ToJson(_db.SaveChanges()); 
     } 

     public HttpResponseMessage Delete(int id) 
     { 
      _db.TWebSitePostsSet.Remove(_db.TWebSitePostsSet.FirstOrDefault(x => x.PostId == id)); 
      return ToJson(_db.SaveChanges()); 
     } 
    } 
} 
+0

.... –

+0

エンドポイントで休息コールを識別するためにhttpsの動詞を逃した> [HTTPGET] [HttpPost] ..... –

+0

私はだろうが、それは多くのコードであり、api url自体が404に直接行くと404が返ってくる理由とは無関係です。 –

答えて

1

変更このコード[RoutePrefix("api/postapi")][Route("api/postapi")]に、それは違いを作るかどうかを確認します。
このページをチェックしてください。あなたはここに正しい答えを与えるために、API呼び出しのための角度のコードを配置する必要が Attribute routing webapi-2

+0

あなたは詳細を教えてください*なぜ*これはOPの問題を解決しますか? –

+0

これは実際に動作します。 2番目のPaulの質問 - なぜこれが機能するのですか?また、私のコードはそれを必要とし、チュートリアルコードはチュートリアルのコードとほとんど同じですがなぜですか? –

関連する問題