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)
  • Have you tried this method: https://www.dotnettips.info/courses/topic/5/3697d84b-8c79-4722-875f-a35920fe88ba – VahidN Jun 22 '18 at 06:48
  • @VahidN Thanks for replay and your useful topics at dotnettips.info...I implement IoC as you explain but it gets error while using web api controllers...I post a new question about it : [using-structuremap-with-mvc-and-webapi-together](https://stackoverflow.com/questions/50988619/using-structuremap-with-mvc-and-webapi-together) – Abolfazl Davoodi Shandiz Jun 22 '18 at 13:13

1 Answers1

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

VahidN
  • 18,457
  • 8
  • 73
  • 117
  • 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. – Abolfazl Davoodi Shandiz Jun 27 '18 at 19:08
  • When you are using attribute routing your "/api" won't be used anymore. Start learning about attribute routing here https://www.dotnettips.info/post/1660 – VahidN Jun 28 '18 at 03:56
  • Thank you...I'm beginner in MVC and this is very helpful. – Abolfazl Davoodi Shandiz Jun 28 '18 at 19:18