I am trying to get any response from my API. I have read a lot but any way get 404 (The resource cannot be found.) Here is my code
Web.config:
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" defaultUrl="~/Home/Index">
</forms>
</authentication>
Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
WebApiConfig.Register(GlobalConfiguration.Configuration);
}
WebApiConfig.cs
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 }
);
// webapi return json
// http://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
ApiController:
public class MyAppApiController : ApiController
{
public HttpResponseMessage Test()
{
return Request.CreateResponse(HttpStatusCode.OK, new { Success = true, bbb = "assssdfv[spld[ssadf[ps" });
}
public HttpResponseMessage Login(LoginViewModel loginVM)
{
var url = this.Url.Link("Default", new { Controller = "AccountController", Action = "Login", model = loginVM});
return Request.CreateResponse(HttpStatusCode.OK, new { Success = true, RedirectUrl = url });
}
}
I am using Google Chrome Simple REST client for testing:
- Run my VS as administrator
- Debug my app (localhost:12345 opens with my site)
Paste in Google Chrome Simple REST client:
http://localhost:12345/api/Test
and run GET => result 404 (not found resource)
Paste in Google Chrome Simple REST client:
http://localhost:12345/api/Login
fields data and headers are void and run POST => result 404 (not found resource)
Where is mistake at my code?
Update :
I have updated :
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//removed this: WebApiConfig.Register(GlobalConfiguration.Configuration);
}
and after this my code that is
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
works
But I still getting 404 error - But the error is new:
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:12345/api/Login'.","MessageDetail":"No type was found that matches the controller named 'Login'."}
My controller name is
myProjApiController :ApiController
but
http://localhost:12345/api/myProjApiController/Login'
result in :
404 No type was found that matches the controller named 'myProjApiController'