0

I use both of MVC and WebApi in my application. For implementing IoC i use structure map sample package(StructureMap.WebApi2)

In MVC controllers everything is ok but when i use web api, for example post something to server i get this error:

[HttpException]: The controller for path '/api/Activity/AddActivity' was not found or does not implement IController.
   at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
   at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

This is AddActivity action:

[HttpPost]
[ActionName("AddActivity")]
public HttpResponseMessage AddActivity(ActivityDTO activityDTO)
  {
      ActivityDTO data = activityDTO;
      HttpResponseMessage response = new HttpResponseMessage();

      data.UserID = User.Identity.GetUserId();

      int id = _activityServices.Add(data);

      if (id != -1)
      {
         response = this.Request.CreateResponse(HttpStatusCode.OK, id);
      }
      else
      {
         response=this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Activity doesn't save.");
      }

       return response;
  }

I check the path of action and it's correct.

can you help me?

thanks

UPDATE 1:

WebApiConfig.cs

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

        var container = IoCFactory.Container;

        GlobalConfiguration.Configuration.Services.Replace(
            typeof(IHttpControllerActivator), new IoCWebApiControllerFactory(container));

        config.MapHttpAttributeRoutes();

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

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

AjaxRequest:

$.post("/api/Activity/AddActivity", data)
2

1 Answer 1

1

Your issue is not the dependency injection. Put a break-point on the constructor and check if the injected dependencies are not null. If those are not null, your setup is correct.

You have routing issues. When you are using [ActionName("AddActivity")] you should have a routing defined like this:

routes.MapHttpRoute(
   name: "ActionApi",
   routeTemplate: "api/{controller}/{action}/{id}",
   defaults: new { id = RouteParameter.Optional } );

The default routeTemplate of web-api doesn't have the {action} part. Also you shouldn't use ActionName here, because it has a different purpose. Replace it with the Route attribute to use the attribute routing capabilities. More info

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

3 Comments

I change the [ActionName("AddActivity")] to [Route("AddActivity")] but i get an exception in GetControllerInstance method...it says Page not found: /api/Activity/AddActivity...i change the request path,route attribute and web api config template but nothing happened and i get that error again.i put the ajax request code in question.
When you are using attribute routing your "/api" won't be used anymore. Start learning about attribute routing here dotnettips.info/post/1660
Thank you...I'm beginner in MVC and this is very helpful.

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.