nick gowdy
nick gowdy

Reputation: 6511

Adding Web API 2 manually - getting 404

I've returned to a solution that was put on hold and when I originally created it, I didn't tick Web API. I've added it manually but when I call my URL i'm recieving a 404. I suspect I am missing some configuration but I'm not sure.

My web api controller

[RoutePrefix("search")]
    public class SearchController : BaseWebApiController
    {
        private readonly IAmtProxy _amtProxy;

        public SearchController(IAmtProxy amtProxy)
        {
            this._amtProxy = amtProxy;
        }

        [HttpGet]
        [Route("supportticket/{id}")]
        public HttpResponseMessage GetSupportTicket(int id)
        {
            try
            {
                var result = _amtProxy.GetSupportTicketById(id);
                return GetResponse(result);
            }
            catch (Exception e)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message);
            }
        }

        [HttpGet]
        [Route("supportticket")]
        public HttpResponseMessage GetAllSupportTickets()
        {
            try
            {
                var result = _amtProxy.GetAllSupportTickets();
                return GetResponse(result);
            }
            catch (Exception e)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message);
            }
        } 
    }

Example of url that returns 404

http://localhost:60541/search/supporticket/1

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /search/supporticket/1

Application_start in global.asax.cs has this code:

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            Bootstrapper.Initialise();
        }

I also have my WebApiConfig

 public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();

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

Finally with nuget I got web api 2 and it installed successfully.

Does anyone know what I could be missing?

Upvotes: 1

Views: 65

Answers (1)

Anish Patel
Anish Patel

Reputation: 4392

Your config looks fine.

It appears that you have misspelled the url:

http://localhost:60541/search/supporticket/1

It should be:

http://localhost:60541/search/supportticket/1

According to your implementation:

[Route("supportticket/{id}")]

Upvotes: 1

Related Questions