I am learning Angular 2 and trying to get it running on an ASP MVC setup. I am following this very excellent guide: https://www.codeproject.com/Articles/1181888/Angular-in-ASP-NET-MVC-Web-API-Part
I modified it very slightly. The original is supposed to manipulate a list of names obtained from the database through the API endpoint api/userapi. I changed it up so that it becomes a list of posts (like for a blog or something) from the endpoint api/postapi. The Angular part of the project is working so far. It's the ASP.NET part of it that's not working properly.
Looking at the configuration of the original project (which I downloaded from that link and tested in Visual Studio 2017 to be fine) and comparing it to my experiment, for the life of me I could not spot where I may have misconfigured it.
What could be the problem here? Here's my code for the Web API part:
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());
}
}
}