I have created a MVC web API with MVC 5 controller for managing CRUD operations.
Here is the snippet of my API controller:
public class UserController : ApiController
{// GET api/<controller>
public IEnumerable<User> GetAllUsers()
{
UserRepository urepo = new UserRepository();
return urepo.UserList(0);
}
public User GetUserByID(int userID)
{
UserRepository urepo = new UserRepository();
var userDetails = urepo.UserList(userID);
return (from n in userDetails where n.UserId == userID select n).SingleOrDefault();
} }
and here is my WebAPIconfig file
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
When I access this controller from URL say localhost/api/User/GetAllUsers it is returning all users as expected.
But if i try to access in other way say localhost/api/User which is not there , will throw file not found error and show default error page.
what i need is instead of this default error page custom error page need to be displayed. How this can be achieved?
Let's say that you are developing a HTTP RESTful application using ASP.NET Web API framework. In this application you need to handle HTTP 404 errors in a centralized location. This Blog might help you in finding what you need.