1

There were many suggested questions/answers when adding this. I went through a bunch and none seemed to help.

I am sure I am missing something simple, just can't see it.

I have an existing ASP.Net MVC (V5) Site. It's a single page.

I am adding an API Controller (WebAPI 2) to it to handle some ajax posts.

I can't seem to get past the 404 Error when trying to post to my controller.

I am also using Attribute Routing.

Here are the parts of the code I believe you will need to see:

API Controller

[RoutePrefix("api/services")]
public class ServicesController : ApiController
{
    [HttpPost]
    [Route("contact")]
    public HttpResponseMessage SendContact(ContactModel model)
    {
        return Request.CreateResponse(HttpStatusCode.OK, true);
    }

}

WebApiConfig - couple of parts commented out as i try things.

public class WebApiConfig
{
    public static void Register(HttpConfiguration configuration)
    {

        configuration.MapHttpAttributeRoutes();


        //configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
        //    new { id = RouteParameter.Optional });
    }
}

And my global.cs - again commented out things i've tried

AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
//WebApiConfig.Register(GlobalConfiguration.Configuration);
BundleConfig.RegisterBundles(BundleTable.Bundles);
GlobalConfiguration.Configure(WebApiConfig.Register);

When attempting to post to the api i receive:

POST http://localhost:8085/api/services/contact 404 (Not Found)

Any idea what I am missing?

0

1 Answer 1

2

Try to put

GlobalConfiguration.Configure(WebApiConfig.Register);

before

RouteConfig.RegisterRoutes(RouteTable.Routes);

so it should be:

        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
Sign up to request clarification or add additional context in comments.

1 Comment

God dammit... Thank you! I KNEW it was something simple! Thanks again.

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.