2017-09-26 95 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

你必須把這裏的角代碼API調用端點提供正確的答案.... –

+0

你錯過了HTTPS動詞來識別REST調用> [HTTPGET] [HttpPost] ..... –

+0

我想,但這是很多代碼,並且它與爲什麼api url本身返回404如果直接進入它無關。 –

回答

1

改變此密碼[RoutePrefix("api/postapi")][Route("api/postapi")],看看是否有差別。
並查看此頁面。 Attribute routing webapi-2

+0

請您詳細說明*爲什麼*這解決了OP的問題? –

+0

這實際上有效。我第二個保羅的問題 - 爲什麼這個工作,以及爲什麼我的代碼需要它,而教程代碼與本教程中的代碼幾乎完全相同? –