Asp.Net Core "Web API" Routes Returning 404 Not Found

16,411

Solution 1

You should apply Route attribute to controller methods (actions), and you can also point the http verb associated with the routes. Take a look at this documentation, it will help you.

Solution 2

This happened to me with the newest Core 3.1 Asp.Net Core Web App template. The issue was that the default wireup was this:

app.UseEndpoints(endpoints => {
    endpoints.MapRazorPages();
    endpoints.MapControllers();
});

But, it needed to be this:

app.UseEndpoints(endpoints => {
    endpoints.MapRazorPages();
    endpoints.MapControllers();
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}"
    );
});

The previous answer lead me to the solution, but I wanted to post a more explicit answer in case it was helpful to others.

Share:
16,411

Related videos on Youtube

TheDudeAbides
Author by

TheDudeAbides

Updated on June 04, 2022

Comments

  • TheDudeAbides
    TheDudeAbides almost 2 years

    On Windows 7 Dev machines that have Asp.net Core 2, all our API specific routes that return data (not Views) work.

    A developer recently got the API setup on his new Windows 10 machine. When we run the routes in Postman or swagger, he can only get one particular GET route to work (extra tidbit - so happens this is the only route that does not call EntityFramework).

    All other routes return a 404 Not Found as if the URLs don’t exist. It’s not our code returning the 404, it’s the platform itself.

    None of our code is being executed since the 404 is being returned by the server, so no useful logging either.

    I also deployed and tested it on a Win server 2016 machine, and getting the same exact issue.

    The last thing I did on this server was install the Asp.net Core 2 SDK but had no effect.

    some code:

    [Produces("application/json")]
    [Route("api/v1/Signatures/Request")]
    public class SignatureRequestController : ControllerBase
    
    [HttpPost]
    [Route("")]
    public async Task<SignatureRequestBaseResponse> 
    CreateSignatureRequestAsync([FromBody]SignatureRequest signatureRequest)
    

    example POST url:

    http://localhost/My.API/api/v1/signatures/request/
    

    example json body:

    {
        "clientApplicationInstanceId" : "4318704B-7F90-4CAE-87A9-842F2925FE45",
        "facilityId" : "PT",
        "contact": "[email protected]",
        "documentInstanceGuid" : "cc46c96f-cd78-448e-a376-cb4220d49a52",
        "messageType" : "1",
        "localeId": 1,
        "field": 
                    {
                        "fieldId": 45,
                        "signatureType": "4",
                        "displayName": "Short Display Name from iMed4Web",
                        "signerNameCaption": "signer name caption",
                        "signerAddressCaption": "address caption",
                        "signerCityStateZipCaption": "city state zip caption"
                    },
        "documentPreviewHtml": "too long to show..."
    }
    
    • NightOwl888
      NightOwl888 about 6 years
      Post the rest of your routing configuration (Startup.cs and the attribute routes in this controller). In routing the first match wins, so it is likely there is another route that is matching that you didn't intend to.
    • TheDudeAbides
      TheDudeAbides about 6 years
      Turned out to be something with Serilog. But only on Windows Server 2016. Sigh.
  • Luís Antunes
    Luís Antunes about 6 years
    @TheDudeAbides are you using default routes, and only route attributes? Is the any mapRoutes in your startup class?
  • TheDudeAbides
    TheDudeAbides about 6 years
    using all attribute routes for api. There is the default home controller one for the view. We deleted the HomeController and View. I could probably remove that route now and see
  • Luís Antunes
    Luís Antunes about 6 years
    I think that must have to be the route matching. Can you paste here a snippet of your Startup class where you do, and if you do, some route mapping? When a request arrives, the route collection is processed in order, the order matters here.