Reputation: 1708
this is my first time using MVC and I have been stuck trying to get the webApi routing to work.
I have a simple controller which looks like this
namespace XRM.Controllers
{
public class PipelinesWebApiController : ApiController
{
static List<XRM.Models.XRM_PipelineStages> stages = new List<Models.XRM_PipelineStages>
{
new Models.XRM_PipelineStages() { stageName = "test" },
new Models.XRM_PipelineStages() { stageName = "test2" },
new Models.XRM_PipelineStages() { stageName = "test3" }
};
public IEnumerable<XRM.Models.XRM_PipelineStages> Get()
{
return stages;
}
public XRM_PipelineStages Get(int id)
{
return stages.FirstOrDefault(s => s.ROWUID == id);
}
}
}
WebApiConfig:
namespace XRM {
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
Global.asax.cs
namespace XRM {
public class MvcApplication : HttpApplication {
protected void Application_Start() {
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
DevExtremeBundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
And whenever I try to access using
http://localhost:58061/api/PipelinesWebApi
I get the error message:
No HTTP resource was found that matches the request URI 'http://localhost:58061/api/PipelinesWebApi'.
I have checked that I have config.MapHttpAttributeRoutes();
in my WebApiConfig.cs too.
I really don't know why I can't seem to reach it.
Upvotes: 1
Views: 90
Reputation: 5311
Your code worked fine at my end. I am not able to reproduce this issue. Anyway, you can try one more way of routing as shown below:
Change the routeTemplate
as "api/{controller}/{action}/{id}"
in WebApiConfig.cs
and call the URL along with action name like api/PipelinesWebApi/Get
Upvotes: 1