Request is always null in web api?
asp.net-mvc-4, asp.net-web-api, c#
Solution
This is expected - you're in the controllers constructor. The controller hasn't been initialized with the actual request yet. Try something like the following:
protected string EntityName
{
get { Request.GetRouteData().Values["controller"] as string; }
}
That should be accessible after the constructor has run, and when you're in a subclass method.
Problem
I have a Web API with a Base Controller, I wanna get requested controller name from `Request.GetRouteData().Values["controller"]`, as the following code : ``` public class BaseApiController : ApiController { protected string EntityName; public BaseApiController() { //Request is null EntityName = Request.GetRouteData().Values["controller"] as string; } } ``` But Request is always null in above code. What's wrong with above code?