6

My Web API on an Asp.Net MVC web app is returning 404 error when receiving requests that don't specify any controller. The calls that are returning 404 error are:

https://myWebApp/api/

The goal would be to handle these type of requests without returning error and return something like "true" or "new EmptyResult()".

Current routing for the Web API includes the following in WebApiConfig.cs

public static class WebApiConfig
    {                
        public static void Register(HttpConfiguration config)
        {   
            config.Filters.Add(new IdentityBasicAuthenticationAttribute());

            config.MapHttpAttributeRoutes();

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

While I have routes explicitly defined for each API controller:

[IdentityBasicAuthentication]
[Authorize]
public class myApi1Controller : ApiController
{
    [HttpGet]
    [Route("api/myApi1")]
    public string Get(string id) { ... }
}

I have tried to route these calls to a default API or MVC controller without success.

My current RouteConfig for the MVC app is:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "myWebApp.Controllers" }
        );
    }
}

The order in which these are called is:

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

2 Answers 2

4

Create a controller to handle that route so it does not fail with not found

Like

[RoutePrefix("api")]
public class MyEmptyController : ApiController {
    //GET api
    [HttpGet]
    [Route("")]
    public IHttpActionResult Get() { 
        return StatusCode(HttpStatusCode.NoContent); //204
    }
}

Using attribute routing as it is already enabled via config.MapHttpAttributeRoutes(); in WebApiConfig

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

Comments

0

Your route config is different from your WebApi config and I don't know the order that you are calling them.

in your RouteConfig:

  url: "{controller}/{action}/{id}",

which means: http://localhost:PORTNUMBER/myApi1/ACTION

in your WebApiConfig

routeTemplate: "api/{controller}/{id}",

(method and id are optionals) which means: http://localhost:PORTNUMBER/api/myApi1/?id=value

change your WebApiConfig, and you even will be able to avoid using the Route tag in your controller :

  [IdentityBasicAuthentication]
    [Authorize]
    public class myApi1Controller : ApiController
    {
        [HttpGet]
        public string Get(string id) { 
    return "works!";
     }
    }

Edit: Keep every thing the same, Invoke your default GET method from: http://localhost:PORTNUMBER/api/myApi1

3 Comments

Thx! I added the order in which they are called. Can you please detail the changes I should do on WebApiConfig ?
check my update, since we start by register the web api and only then the routes , the pattern is:localhost:port{controller}/{action}/{id} where id and Action are optional
"change your WebApiConfig, and you even will be able to avoid using the Route tag in your controller :" I'm sorry, I didn't get that. What should I change in WebApiConfig?

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.