0

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());
        }
    }
}
7
  • you have to put here angular code for api call to give correct answer.... Commented Sep 26, 2017 at 1:48
  • in endpoints you missed https verb to identify rest call > [HttpGet] [HttpPost]..... Commented Sep 26, 2017 at 1:50
  • I would but it's a lot of code and it's unrelated to why the api url itself is returning 404 if you go directly to it. Commented Sep 26, 2017 at 1:51
  • not want all need api call point, just post Get method call point Commented Sep 26, 2017 at 1:51
  • 1
    You want to call your API via api/postapi (post - singular). But by convention (default route + http verbs) your example should be accessible at api/postSapi (posts - plural) because of you controller naming. Commented Sep 26, 2017 at 6:09

1 Answer 1

1

Change this code [RoutePrefix("api/postapi")] to [Route("api/postapi")] and see if that makes a difference.
And check out this page. Attribute routing webapi-2

Sign up to request clarification or add additional context in comments.

2 Comments

Could you please elaborate why this solves the OPs issue?
This actually works. I second Paul's question - why does this work, and also why does my code require it and the tutorial code does't when it's pretty much identical to the code in the tutorial?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.