I have visual studio 2012 installed with mvc4 using .net framework 4.5. Now I want to use webapi2 with attribute writing and i want my hlep page show all the endpoints prope
A problem I ran into was related to the ordering in Application_Start(). Note the order of Web API configuraton below:
This does NOT work
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
GlobalConfiguration.Configure(WebApiConfig.Register);
}
This does work
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
This was not your case (as is apparent from your sample code), but please do remember to end the Controller class name with Controller.
Else it won't be picked up by config.MapHttpAttributeRoutes();.
I had this problem too and after a long search I realized that I was using System.Web.Mvc.RouteAttribute instead of System.Web.Http.RouteAttribute
After correcting this and using config.MapHttpAttributeRoutes() everything worked fine.
In my case, VS create my controller with the name
TestController1
I dont know why he put this number "one" in the end of name, but remove and will work.
In my case, I was missing full custom path in attributes. I was writing only custom action name without 'api/'. So that was my mistake. My scenario was, WebApiConfig template code:
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
my incorrect way of route
[RoutePrefix("myapps")] // wrong code
public class AppsController : BaseRestAPIController
{
[HttpPost]
[Route("getapps")]
public ResponseData GetAppList()
{
Correct way
[RoutePrefix("api/myapps")] // correct way. full path start from 'api/'
public class AppsController : BaseRestAPIController
{
[HttpPost]
[Route("getapps")]
[Route("api/myapps/getapps")] // you can use full path here, if you dont want controller level route
public ResponseData GetAppList()
{
In my case following line was creating problem, just commented it and everything start working
config.MapHttpAttributeRoutes();
Comment it in WebApiConfig.cs file