3

I'm new to web api, so I trying to catch action (action with name Test) in web api controller with no success, always get The resource cannot be found. 404 error

This is my controller:

public class ReadController : ApiController
{
    [HttpGet]
    public IHttpActionResult Test()
    {

        return Ok();
    }
    // GET: api/Read
    public IEnumerable<string> Get()
    {

        return new string[] { "value1", "value2" };
    }

    // GET: api/Read/5
    public string Get(int id)
    {
        return "value";
    }

    // POST: api/Read
    public void Post([FromBody]string value)
    {
    }

    // PUT: api/Read/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE: api/Read/5
    public void Delete(int id)
    {
    }

This is my WebApiConfig class

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

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

Trying with this call

https://localhost:44300/api/Read/Test

https://localhost:44300/api/Test

in both cases I get 404 error

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Optimus
  • 49
  • 4
  • are you sure your `WebApiConfig.Register` is being called? (Generally from within your `Application_Start` in global.asax) – Kritner May 14 '15 at 19:46
  • 1
    I highly recommend using Attribute Routing instead of magic string routes. http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 – Claies May 14 '15 at 19:54
  • Yes, WebApiConfig.Register is called. – Optimus May 14 '15 at 19:55
  • you should add routing attribute to your test method. something like this [Route("test")]. – Prashant May 14 '15 at 19:56
  • I tried with route attribute [Route("test")] and route prefix above controller [RoutePrefix("api")] I got same result 404 – Optimus May 14 '15 at 20:05
  • what url are you trying to call now.. https://localhost:44300/api/Test – Prashant May 14 '15 at 20:08
  • yes, that url call localhost:44300/api/Test – Optimus May 14 '15 at 20:11
  • [RoutePrefix("api")] public class ReadController : ApiController { [HttpGet] [Route("test")] public IHttpActionResult Test() { return Ok(); } ...... if this is what your controller looks like you are doing it right, it works for me. – Prashant May 14 '15 at 20:17
  • it just looks like my controller..... – Optimus May 14 '15 at 20:26
  • This is my Global.asax file ` public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //GlobalConfiguration.Configuration.MapHttpAttributeRoutes(); GlobalConfiguration.Configure(WebApiConfig.Register); } }` – Optimus May 14 '15 at 20:28
  • it looks like you first created a MVC application and now trying to add a web api endpoint to it right? – Prashant May 14 '15 at 20:57
  • if you want to web api only project , there is a web api template try that. – Prashant May 14 '15 at 20:59
  • yes you are right. What is the problem with that? – Optimus May 14 '15 at 20:59
  • If its a web api its a service so its hosted separately from the web solution where it is consumed, that how we should do. But to answer you its definitely some missing references, you cannot just add routing in MVC application for web api to get it working. Probably you can setup a web api project see if it works and figure what the differences between the two. – Prashant May 14 '15 at 21:12
  • Posting code in the comments won't help much. You can edit your question accordingly. – emerson.marini May 14 '15 at 21:18

0 Answers0