简体   繁体   中英

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

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": "me@my.com",
    "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..."
}

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 t his documentation , it will help you.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM