Search code examples
asp.netiisasp.net-web-api2httpmodule

404 when WebAPI 2 Controller is called


i have developed a simple WebAPI 2 Controller with only one Controller and some simple GET Methods. I have created for this a Visual Studio 2015 Emtpy ASP.Net Web Application and added a WebAPI 2 Controller to it.

I have published it to a Windows 2012 R2 Machine with IIS 8.5 and ASP.Net 4.0 installed on it. It has thelatest updates etc.

When i call the Method from a browser i get an error 404 - 0x80070002 Notification: MapRequestHandler

You can download my solution here: VS2015 WebAPI2 Solution

This is the error message:

enter image description here

This is my webapplication:

enter image description here

These are the handlers:

enter image description here

enter image description here

This is the WebApplication configuration:

enter image description here

This is the Application Pool configuration:

enter image description here

These are the ISAPI filters:

enter image description here

Has anybody an idea about the possible reason why this it not working?

This is my controller:

using System.Collections.Generic;
using System.Web.Http;

namespace WebApi2Routes1.Controllers
{
    // [Controller wird api/Default in diesem Fall genommen, also Controller Name ohne 'Controller'
    [RoutePrefix("api/[controller]")]
    public class BuecherController : ApiController
    {
        // GET: api/buecher/default
        // [HttpGet]
        [Route("")]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/buecher/show
        [Route("show")]
        public string Show()
        {
            return "Show";
        }

        [Route("value/{id:int}")]
        public string GetValue(int id)
        {
            return "value" + id.ToString();
        }


    }
}

This is my web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  Weitere Informationen zum Konfigurieren Ihrer ASP.NET-Anwendung finden Sie unter
  http://go.microsoft.com/fwlink/?LinkId=301879
  -->
<configuration>
  <appSettings></appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.webServer>
    <modules>
      <remove name="UrlRoutingModule-4.0" />
      <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
      <remove name="OPTIONSVerbHandler"/>
      <remove name="TRACEVerbHandler"/>
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler"
        preCondition="integratedMode,runtimeVersionv4.0"/>
  </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>

I this is the url which throws the error:

http://localhost:8080/api/buecher

and

http://localhost:8080/api/buecher/show

Update 1:

I also have a global.asax

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Routing;

namespace WebApi2Routes1
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
    }
}

and a WebApiConfig.cs

using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web.Http;

namespace WebApi2Routes1
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web-API-Konfiguration und -Dienste

            // Web-API-Routen
            config.MapHttpAttributeRoutes();

            // var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
            // jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        }
    }
}

Solution

  • Solved by myself. There where two issues:

        [RoutePrefix("api/[controller]")]
    

    [controller] as a token does not work! You have to replace it with a static string like "books" or anything else.

    The second issue was that methods need an explicite HHTPGET or HTTPPOST etc. This does not work

        [Route("show")]
        public string Show()
        {
            return "Show";
        }
    

    This works

        [Route("show")]
        [HttpGet]
        public string show()
        {
            return "Show";
        }
    

    Only if the Method itself is called Get() or Post() or ... then there is no need for [HttpGet].

    After replacing the [controller] token in RoutePrefix by a static value and adding [HTTPGET] to the method it worked!