Reputation: 4245
I have been struggling a WebApi2 setup based on Attribute Routing and I have run out of ideas what can be the problem. The following code is a newly created WebApi project by Visual Studio 2015. There are no changes in it. It simply does not work.
The response says the following:
<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://.../services/webapi2/api/dummies/dummymethod'.
</Message>
<MessageDetail>
No type was found that matches the controller named 'dummies'.
</MessageDetail>
</Error>
What I did so far:
Thanks for any help in advance!
DummyController.cs
using System.Web.Http;
namespace WebApi2.Controllers
{
[RoutePrefix( "Dummies" )]
public class Dummy : ApiController
{
[Route("dummymethod")]
public string Get()
{
return "asdasd";
}
}
}
WebApiConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace WebApi2
{
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 }
);
}
}
}
Installed packages:
<?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" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
</packages>
Upvotes: 1
Views: 4291
Reputation: 247088
You are using attribute routing with a route prefix of only dummies
so it would map to this URL
http://.../services/webapi2/dummies/dummymethod
So either use the above URL or update your route prefix to include api
to match the URL used in your example
namespace WebApi2.Controllers
{
[RoutePrefix( "api/Dummies" )]
public class Dummy : ApiController
{
//GET api/dummies/dummymethod
[HttpGet]
[Route("dummymethod")]
public string Get()
{
return "asdasd";
}
}
}
The above matches the request URI http://.../services/webapi2/api/dummies/dummymethod
Upvotes: 4