WebApi and MVC Routing not working on Server 2012 IIS 8.5 (404.0 Error)
Asked Answered
M

0

6

I have an application using .NET Framework 4.7.1 with WebAPI and MVC routes defined in it. They both work when debugging locally with IISExpress, but when I deploy to the Development server (Windows Server 2012R2 with IIS 8.5), all I get is a 404.0 error for every route. I have put a static test.aspx file in the root folder of my IIS application and that returns correctly so it seems that IIS is just not picking up the routing. I have tried everything under the sun that I can Google and nothing has fixed it.

Here is my code for RouteConfig.cs:

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

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
           name: "Default",
           url: "{controller}/{action}/{id}",
           defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
       );

    }
}

Here is my code for WebApiConfig.cs:

using System.Web.Http;
using System.Net.Http.Formatting;

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.IgnoreRoute("axd", "{resource}.axd/{*pathInfo}");

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Formatters.Clear();
        config.Formatters.Add(new JsonMediaTypeFormatter());

        // configure additional webapi settings here..
    }
}

My Web.config includes the following:

<modules runAllManagedModulesForAllRequests="true"/>
<!-- Show detailed error messages -->
<httpErrors errorMode="Detailed" />

<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>

I have checked and .NET Framework 4.7.1 is installed on the server. Any help would be greatly appreciated.

Mckeown answered 18/4, 2018 at 20:53 Comment(6)
Is your app pool set to use the right framework?Demure
Yes, the app pool is set to .NET CLR Version v4.0.30319 and it is in Integrated mode.Mckeown
FYI...I deployed the EXACT same code to a Windows Server 2008R2 instance and all I had to change in the Web.config files was the .NET framework version from 4.7.1 to 4.5.2 and it worked just fine. So, it definitely seems to be some configuration with my 2012 server or IIS, but I have no idea what.Mckeown
No news on this? Did you get it to work? I'm facing same problem.Chap
@TylerWright any update?Aa
@gina-marano, it has been a while and I can't remember what resolved the issue for me, but it ended up being a bug in my code. For some reason, it worked on Server 2008, but failed on 2012, but there was something that was causing a 500 error in my code that I ended up finding and fixing. Sorry!Mckeown

© 2022 - 2024 — McMap. All rights reserved.