简体   繁体   中英

ASP.NET Core Web Api controller end points are all returning 404 error

I have a ASP.NET Core 3.1 Web Api project that 9 ApiControllers with several endpoints per controller. Each controller endpoint communicates with a Unit of Work / Repository pattern built on Dapper ORM.

All controllers class headers look like this:

[Route("api/[controller]")]
[ApiController]
public class AuthController : ControllerBase
...

This Api communicates with a Xamarin Android project to send and receive data.

This solution has been working very well since the early spring. I made a couple changes to the repository layer to modify a query, and the code compiles properly. When I run the application now in both localhost and at a web host server, ALL endpoints tested from Postman return a 404 error. Nothing was changed in Nuget packages, appsettings.json or launchsettings.json.

I have been trying to figure this out for 2 days and really need some help. I have reviewed all of related posts that I can locate that has core but I have not seen a match to my problem.

What other info can I provide?

Ryan

UPDATE 1 Here is a sample method that gets called to authenticate a user: This url would be called like 'https://localhost:44365/api/auth/authenticate'

[HttpPost("authenticate")]
        public async Task<ActionResult<Login>> Authenticate([FromBody] Login login)
        {
            // login will come in from the device with basic auth
            string token = string.Empty;
            string username = login.UserName;
            string password = login.Password;
            int product = login.Product;
            string deviceName = login.DeviceName;
            try
            {
                bool isAuth = await _unitOfWork.Logins.GetAuthenticateUser(username, password, product);
                if(isAuth == false)
                {
                    return StatusCode(401);
                }

                //check for preference record
                bool exists = await _unitOfWork.Sessions.CheckForDevice(deviceName, product);
                if(!exists)
                {
                    // auto add the device
                    Prefs prefs = new Prefs();
                    SecurityUser user = await _unitOfWork.Logins.GetUserInfo(username, product);
                    prefs.deviceName = deviceName;
                    prefs.product = product;
                    prefs.facilityId = user.facID;
                    prefs.addedBy = username;
                    prefs.active = true;
                    prefs.timeout = "480";
                    int result = await _unitOfWork.Sessions.AddDevice(prefs, product);
                }

                Jwt j = new Jwt(_unitOfWork);
                try
                {
                    login.Token = j.CreateToken(username, deviceName, product);
                    SessionModel session = new SessionModel();
                    session.SessionStart = DateTime.Now;
                    login.SessionStart = session.SessionStart;
                    session.UserName = username;
                    session.Token = login.Token;
                    await _unitOfWork.Sessions.AddSession(session, product);
                }
                catch (Exception ex)
                {
                    //delete session if there is jwt error
                    await _unitOfWork.Sessions.DeleteAsyncByUserName(username, product);
                    var msg = $"Error message from create token - Inner Exception: {ex.InnerException} Exception Message: {ex.Message} Source: {ex.Source}";
                    login.Message = msg;
                    login.UserName = String.Empty;
                    login.Password = String.Empty;
                    //return login object with message 
                    return login;
                }
            }
            catch (Exception ex)
            {
                var msg = $"Error message from Header Auth decode - Inner Exception: {ex.InnerException} Exception Message: {ex.Message} Source: {ex.Source}";
                login.Message = msg;
                login.UserName = String.Empty;
                login.Password = String.Empty;
                return login;
            }
            //return login obj with token, msg, session datetime
            login.UserName = String.Empty;
            login.Password = String.Empty;
            return login;
        }

Update 2 My biggest challenge here is figuring out why the controller endpoints are suddenly returning 404 after 5 months and no modification to the controller code.

Update 3 The Startup Configure method

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

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

        }

I discovered in the appsettings.json file, there was a C# style comment in the json. Removing that comment appears to have fixed the problem.

I had a similar problem with a WebAPI hosted on Azure. I did an upgrade on the NuGET Packages and did a migration to OData Version 8. This suddenly made my queries case sensitive in the filters. Lets say I have a field "State" in my database and I query this with "https://localhost:5555/Adresses?$filter=state eq 'NY'" then is gives me an error because I query for state and not for State. Other thing could be that, in case you use API Management on Azure, that you Swagger file is broken and the links in the API management as not there. Maybe this helps you.

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