0

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:

  1. Run my VS as administrator
  2. Debug my app (localhost:12345 opens with my site)
  3. Paste in Google Chrome Simple REST client:

    http://localhost:12345/api/Test   
    

    and run GET => result 404 (not found resource)

  4. 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'
curiousity
  • 4,703
  • 8
  • 39
  • 59
  • 2
    Why did you comment out the route mapping? If the routes are already defined, what are they? – David Nov 06 '15 at 13:12
  • Since you have commented out the default route, the `api` part of the URL is no longer recognised. Instead try this: `http://localhost:12345/Test` – DavidG Nov 06 '15 at 13:17
  • If I am uncommenting it I have got an Additional information: A route named 'MS_attributerouteWebApi' is already in the route collection. Route names must be unique. Exception =( – curiousity Nov 06 '15 at 13:18
  • Side note: view-models belong in client-side/MVC code, not it your Web API. Consider mapping to DTO – Big Daddy Nov 06 '15 at 13:20
  • http://localhost:12345/Test - not found 404 =( – curiousity Nov 06 '15 at 13:22
  • Possibly have another route named "DefaultApi" defined incorrectly in another App_Start class? – Bill Nov 06 '15 at 13:25
  • Find all "DefaultApi", Subfolders, Find Results 1, Entire Solution, "" D:\Prj\myProj\myProj\App_Start\WebApiConfig.cs(20): name: "DefaultApi", Matching Lines 1 – curiousity Nov 06 '15 at 13:28
  • Are you calling WebApiConfig twice then in your Global.asax.cs? You wouldn't be the first: http://stackoverflow.com/a/24179872/792525 – decates Nov 06 '15 at 13:49
  • No. my Global.asax.cs contains only one method and I have provided its full body – curiousity Nov 06 '15 at 14:03
  • OK, so you've now removed the duplicate. It looks like the URL is not right: the controller name in the URL needs to match the name of the controller class (and you don't need to add 'Controller' in the URL). Try: http://localhost:12345/api/MyAppApi/Login – decates Nov 06 '15 at 14:23
  • localhost:12345/api/MyAppApi/Login - 404.. Perhaps I need register it in RouteConfig? – curiousity Nov 06 '15 at 14:43

1 Answers1

0

You are getting a 404 because the routes are not configured, and you're likely to be getting an error when you uncomment the route config because that code is being called twice and is hence trying to define a route with the same name twice.

You've already said that a project-wide search for 'DefaultApi' only returns the result in WebApiConfig.cs. Try the same search for 'WebApiConfig' to look for multiple calls to it.

Alternatively, have a look at these people having the same issue (duplicate routes):

Community
  • 1
  • 1
decates
  • 3,406
  • 1
  • 22
  • 25