I have a controller that I derived from System.Web.Http.ApiController(Used Nuget package Microsoft.AspNet.WebApi.Core.5.2.3).
I have used RoutePrefix on the controller class and Route Attribute on the Action.
I have two routes configured. One within WebAPiConfig(HttpRouteCollection) and other in RouteConfig(System.Web.Routing.RouteCollection)
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
When I hit a request from Postman. I get 404 error code. And strange thing is the stack trace is:
[HttpException]: The controller for path '/api/Account/IsAuthorized' was not found or does not implement IController.
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
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.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
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)
Below is the controller code:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.Description;
[RoutePrefix("api/Account")]
public class AccountController : ApiController
{
/// <summary>
/// Determines whether this instance is authorized.
/// </summary>
/// <returns>send whether the user is authorized or not</returns>
[AllowAnonymous]
[Route("IsAuthorized")]
[HttpGet]
public HttpResponseMessage IsAuthorized()
{
return Request.CreateResponse(User.Identity.IsAuthenticated);
////return true;
}
}
Global.asax order of registration
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
UnityConfig.RegisterComponents(HttpConfiguration.Config);
WebApiConfig.Register(HttpConfiguration.Config);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
Why the framework is trying to initialize controller via System.Web.Mvc.DefaultControllerFactory.GetControllerInstance
In the same API project, I have another controller that is devired by System.Web.Mvc.Controller and that is working good.
Any pointers will help me a lot.