I have a asp.net MVC 5 project with lots of controllers and web apis. The problem is when i want to access one of the api methods, i get No HTTP resource was found that matches the request URI ... error.
I tried every solution in similar posts but i wasn't lucky. This is my RouteConfig file:
public static void Register(HttpConfiguration config)
{
// Routing configs
config.MapHttpAttributeRoutes();
// Files api routs
config.Routes.MapHttpRoute(
name: "filesApi_1",
routeTemplate: "api/{controller}/{action}/{file}",
defaults: new object[]
{
new { controller = "Files", action ="SaveAttachment"}
}
);
config.Routes.MapHttpRoute(
name: "filesApi_2",
routeTemplate: "api/{controller}/{action}/{fileName}",
defaults:
new { controller = "Files", action = "DeleteAttachment" }
);
}
When i go to htt://localhost:xxxx/api/Files/DeleteAttachment/file_name, i get the error.
My Global.asax.cs is:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Mapper.Initialize(c=> c.AddProfile<MappingProfile>());
}
The controller is:
public class FilesController : ApiController
{
public void SaveAttachment(HttpPostedFileBase file){}
public void DeleteAttachment(string fileName){}
}
It is very strange that if i add attribute routing for web api actions, they work as expected. For example:
[Route("api/Files/DeleteAttachment/{fileName}")]
public void DeleteAttachment(string fileName){}
I don't want write Route for every action because i have lots of actions and it make the code fragile. How can i fix routing in the route table? Thanks.