404 error after adding Web API to an existing MVC Web Application
It's working!!! I didn't want to believe, but guess what, the problem was related with the Global.asax routing order.
While it doesn't work with:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(WebApiConfig.Register); //I AM THE 4th
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
It works with:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register); //I AM THE 2nd
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
Crazy, I know.
Getting 404 error while calling Web API 2 from MVC 5 application
The issue has been solved. It was due to missing WebApiConfig.cs
file in App_Start folder. I have manually added the file.
public 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 }
);
}
}
And configured the Global.asax
like
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
It works for me. Thank you all for your responses.
Adding WebAPI to an existing MVC Project - getting 404 on
Try to put
GlobalConfiguration.Configure(WebApiConfig.Register);
before
RouteConfig.RegisterRoutes(RouteTable.Routes);
so it should be:
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Getting 404 when trying to hit the WebAPI controller method
The error is with the "ApiController" you should use just Controller in your implementation.
There is indeed to particular ApiController class anymore since MVC and WebAPI have been merged in ASP.NET Core. However, the Controller class of MVC brings in a bunch of features you probably won't need when developing just a Web API, such as a views and model binding.
for more see: Is ApiController deprecated in .NET CORE?
ASP MVC Web API endpoint returning 404
Change this code [RoutePrefix("api/postapi")]
to [Route("api/postapi")]
and see if that makes a difference.
And check out this page.
Attribute routing webapi-2
All ASP.NET Web API controllers return 404
One thing I ran into was having my configurations registered in the wrong order in my GLobal.asax file for instance:
Right Order:
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
Wrong Order:
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
WebApiConfig.Register(GlobalConfiguration.Configuration);
Just saying, this was my problem and changing the order is obvious, but sometimes overlooked and can cause much frustration.
Related Topics
Creating Wcf Channelfactory<T>
Best Practice to Make a Multi Language Application in C#/Winforms
Why Can't C# Interfaces Contain Fields
How to Get Current Page Url in MVC 3
Why Does the Contains() Operator Degrade Entity Framework's Performance So Dramatically
Why Are Private Fields Private to the Type, Not the Instance
Mirroring Console Output to a File
Fast and Compact Object Serialization in .Net
Filter/Search Using Multiple Fields - ASP.NET MVC
What Represents a Double in SQL Server
How to Specify Proxy Credentials in Your Web.Config
What Is the Correct Performance Counter to Get CPU and Memory Usage of a Process
Compare Equality Between Two Objects in Nunit
The Provided Uri Scheme 'Https' Is Invalid; Expected 'Http'. Parameter Name: Via