0

Is it possible to navigate to a URL where a web api exists? My efforts suggest no, common sense says it should be. Thus, I guess I have a fault else where.

I've started VS 2015, added a new project (ASP.NET) and chose web api. I have created my api

public class DefaultController : ApiController
{
    public void Index()
    {
        //logic with break points set
    }
}

I also created a 'normal' MVC controller and as such, when I start the project, my web browser shows me the Index file in my Home controller.

Now, I want to navigate to the associated URL of my web api The URL I have for my Home controller (Index view) is

http://localhost:61895/

Therefore my api should be (I think)

http://localhost:61895/api/default/

but I see the following in the browser

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.

And my WebApiConfig is

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

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

edit

I also tried localhost:61895/api/Default/Index but the same issue

How do I debug my API in Visual Studio

2

1 Answer 1

3

The way Web Api works on the routing is different to Mvc Controller.

There is no action part in web api.

The http action have to match the Beginning of the Action or the Attribute. For Example:

public class DefaultController : ApiController
{
    public void GetIndex()
    {
        //logic with break points set
    }
}

or

public class DefaultController : ApiController
{
    [HttpGet]
    public void Index()
    {
        //logic with break points set
    }
}

The the url will be

localhost:61895/api/Default

There is a good example at asp.net:

http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

enter image description here

edit

add some screen shot. I create a new web api project, and create new controller default:

public class DefaultController : ApiController
{
    public void GetIndex()
    {
        var a = "a";
    }
}

Then my url:

http://localhost:4349/api/default

as you can see the break point get hit.

break point hit

As per discussion in comments, the issue was the Global.asax.cs file

The order matters, and must in the following order

 public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

GlobalConfiguration.Configure(WebApiConfig.Register);

has to be before

RouteConfig.RegisterRoutes(RouteTable.Routes);

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

17 Comments

So, how do I debug the code in visual studio? +1 as this is clear :)
@MyDaftQuestions Put the break point in the begging of GetIndex() method (like you would do in any other application), and run VS in debug mode. Then navigate to the url, then it should stop at the break point
I added the extra GetIndex and added the [HttpGet] to my original Index method - I then navigate to localhost:61895/api/Default - it doesn't help
@MyDaftQuestions you dont need both. just either one with [HttpGet] or one called GetIndex
I tried with [HttpGet] and my original Index, and then I removed this and tried only with GetIndex (and no attribute). Same issue
|

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.