0
votes

TL;TD I've created a new WebApi2 Application and removed all the default MVC guff so just WebApi guff remains. Why isn't it working.

I've created a Web Api 2 project and don't need any non Web Api functionality so I removed it prior to creating my WebApi route and controller. No matter how I try to access it, I cant hit my new web api controller action. Code snippets below;

Global.asax

public class WebApiApplication : HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    }
}

WebApiConfig.cs

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

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{controller}/{action}/{id}"
        );
    }
}

TestController.cs

public class TestController : ApiController
{
    public IEnumerable<TestItem> Get()
    {
        var updates = new List<TestItem>()
        {
            new TestItem()
            {
                Title = "Testing Testing",
                Content = "Testing Content",
                Date = DateTime.Now
            }
        };

        return updates;
    }
}

Project Structure

  • App_Start
    • FilterConfig.cs
    • WebApiConfig.cs
  • Controllers
    • TestController.cs
  • Models
    • TestItem.cs
  • Global.asax

I am completely at a loss, I'm sure I've missed something obvious.

2

2 Answers

2
votes

Your route is defined as the following:

config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "{controller}/{action}/{id}"
);

This is expecting a route which is in 3 segments; i.e. http://localhost/Test/Get/1. However, you don't have any action which matches this. The only action you have matches http://localhost/Test/Get/.

You could correct this by adding defaults: new { id = RouteParameter.Optional } to your Http Route. However, I highly encourage you to consider switching to Attribute based Routing instead. With Attribute routing, you use attributes in your controller to manage your routes, rather than using a magic string routing table. For Example:

[RoutePrefix("Test")]
public class TestController : ApiController {

    // http://localhost/Test/Get
    [Route("Get")]
    public IEnumerable<TestItem> Get() { ...
    }

    //http://localhost/Test/Get/1
    [Route("Get/{id}")
    public TestItem Get(int id) { ...
    } 
}
1
votes

It's not possible to see what is causing your WebApi to fail from the supplied code, but this will give you a working WebApi with minimal setup.

A side note, FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters) is for MVC filters and not used by WebApi. You should instead use config.Filters.Add(new SomeFilter()) in your WebApiConfig.csfile.

Make a GET request to http://localhost:80/api/test (or whatever port it is running on) and it will return a list of TestItem in either XML or JSON depending on your clients http headers.

Global.asax.cs

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

WebApiConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

TestController.cs

public class TestController : ApiController
{
    public IEnumerable<TestItem> Get()
    {
        var updates = new List<TestItem>()
        {
            new TestItem()
            {
                Title = "Testing Testing",
                Content = "Testing Content",
                Date = DateTime.Now
            }
        };

        return updates;
    }
}

TestItem.cs

public class TestItem
{
    public TestItem()
    {
    }

    public string Content { get; set; }
    public DateTime Date { get; set; }
    public string Title { get; set; }
}

I have the following nuget packages installed:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
</packages>