All requests to ASP.NET Web API return 404 error

后端 未结 3 625
北荒
北荒 2020-12-03 13:27

I have an ASP.NET MVC 4 web site that includes Web API. The site is developed and tested with Visual Studio 2012 and .NET 4.5 on Windows 8 with IIS Express as web server.

相关标签:
3条回答
  • 2020-12-03 13:47

    I had the same issue in a remote server, but when I execute on a localhost works fine.

    My solution was:

    <system.webServer>
        <modules>
            <remove name="UrlRoutingModule-4.0" />
            <add name="UrlRoutingModule-4.0" 
                type="System.Web.Routing.UrlRoutingModule" preCondition="" /> 
        </modules>
    </system.webServer>
    

    I hope, that this works for you.

    0 讨论(0)
  • 2020-12-03 14:01

    Thanks to Kiran Challa's comment and source code from this answer I was able to figure out that an assembly (the ReportViewer 11 assembly for SQL Server Reporting Services) was missing on the production server.

    Although no ApiController is in this assembly it seems to cause that controllers in assemblies - in this case my Web project's assembly - that are referencing the missing assembly are not found.

    Apparently this behaviour is related to this piece of code from the Web API's DefaultHttpControllerTypeResolver source:

    List<Type> result = new List<Type>();
    
    // Go through all assemblies referenced by the application
    // and search for types matching a predicate
    ICollection<Assembly> assemblies = assembliesResolver.GetAssemblies();
    foreach (Assembly assembly in assemblies)
    {
        Type[] exportedTypes = null;
        if (assembly == null || assembly.IsDynamic)
        {
            // can't call GetExportedTypes on a dynamic assembly
            continue;
        }
    
        try
        {
            exportedTypes = assembly.GetExportedTypes();
        }
        catch (ReflectionTypeLoadException ex)
        {
            exportedTypes = ex.Types;
        }
        catch
        {
            // We deliberately ignore all exceptions when building the cache. If 
            // a controller type is not found then we will respond later with a 404.
            // However, until then we don't know whether an exception at all will
            // have an impact on finding a controller.
            continue;
        }
    
        if (exportedTypes != null)
        {
            result.AddRange(exportedTypes.Where(x => IsControllerTypePredicate(x)));
        }
    }
    

    I don't know if it has to be this way and I am not quite convinced by the comment in the code but this catch ... continue block is rather silent about a possible problem and it took me a huge amount of time and frustration to find it. I even knew that the ReportViewer wasn't installed yet. I tried to install it and dependent assemblies but it was blocked by another running process on the server, so I decided to postpone the installation until I could contact the administrator and focus on MVC and WebAPI testing first - big mistake! Without Kiran's debugging code snippet I had never had the idea that the existence of a ReportViewer.dll could have anything to do with controller type resolution.

    In my opinion there is room for improvement for the average developer like me who doesn't have a deeper knowledge about the inner workings of Web API.

    After installing the missing ReportViewer.dll the problem disappeared.

    Here are questions about the same symptom which might have the same reason:

    • All ASP.NET Web API controllers return 404
    • .Net Web API No HTTP resource was found that matches the request URI
    • http://forums.asp.net/t/1861082.aspx/1?All+controllers+break+404+whenever+I+publish+to+Azure

    Edit

    I have issued a request for improvement on CodePlex:

    http://aspnetwebstack.codeplex.com/workitem/1075

    Edit 2 (Aug 11 '13)

    The issue has been fixed for WebAPI v5.0 RC. See the link above to the workitem and its comments section for details.

    0 讨论(0)
  • 2020-12-03 14:13

    Great resources in this answer, however, I was getting a 404 for what turned out to be a pretty silly reason:

    1. I created a WiX installer for my API project
    2. Installed it on a test VM (virtual machine)
    3. Requested for the a URI that the API should serve
    4. BANG! 404 :(

    It turned out that I had missed packaging and deploying the Global.asax to the root of the virtual directory in my deployment. Adding this here for the benefit of goofies like myself :)

    0 讨论(0)
提交回复
热议问题