locked
Error 404 failed to load resources file when connect from post man to my app .net core 2.2 RRS feed

  • Question

  • User696604810 posted

    Problem

    I have error 404 couldn't load resources when using post man to send post request to authunticationcontroller/PostUserLogin

    I spend more time but in last not worked or not hit breakpoints on action PostUserLogin .

    i move action PostUserLogin to controler MenuController and send post request by post man it worked and breakpoints hitted why this happen

    what is wrong on my code

    I need when put action PostUserLogin on Authunticationcontroller  and send post request to authunticationcontroller/postuserlogin using post man breakpoint not hit .

    when put break point on  menu controller action PostUserLogin then press click button for send post request breakpoint hitted  and it work 

    but when using another controller name Authuntication and put action PostUserLogin on it and i put breakpoints for test it is not sending post by post man  why

    and display error 404 why and hot to solve this problem

    why authunticationcontroller/postuserlogin not working or breakpoint hit

    my code as below

    public class ApiRoutes
    {
    public const string SecurityBase = "Security";
    public static class Security
    {
    public const string GetUserMenus = SecurityBase + "/UserPrograms/{userId}";
    }
    //Api url For Login
    public const string SecurityLogin = "Authuntication";
    public static class Login
    {
    public const string UserLogin = SecurityLogin + "/login";

    }
    }


    public class MenusController : Controller { readonly Services.ISecurityService _SecurityService; public MenusController(Services.ISecurityService securityService) { _SecurityService = securityService; } [HttpGet(Contracts.ApiRoutes.Security.GetUserMenus)] public IActionResult GetUserMenu(string userId) { string strUserMenus = _SecurityService.GetUserMenus(userId); return Ok(strUserMenus); } [HttpPost(Contracts.ApiRoutes.Login.UserLogin)] public IActionResult PostUserLogins([FromBody] Users user) {
    }
    public class MenusController : Controller
    {
    readonly Services.ISecurityService _SecurityService;
    public MenusController(Services.ISecurityService securityService)
    {
    _SecurityService = securityService;
    }
    [HttpGet(Contracts.ApiRoutes.Security.GetUserMenus)]
    public IActionResult GetUserMenu(string userId)
    {
    string strUserMenus = _SecurityService.GetUserMenus(userId);
    return Ok(strUserMenus);
    }
    [HttpPost(Contracts.ApiRoutes.Login.UserLogin)]
    public IActionResult PostUserLogins([FromBody] Users user)
    {
    }

    my question why sending post request to function PostUserLogin on controller Authuntication Not working  why ?

    Friday, September 6, 2019 11:31 PM

All replies

  • User475983607 posted

    This is incorrect...

     authunticationcontroller/PostUserLogin

    Remove "controller"

     authuntication/PostUserLogin

    IMHO, your routing design is very confusing and overly complex.  It was suggested in your duplicate thread to use standard routing.  Why obfuscate the the route with a static property?  There's no benefit unless the goal is to make your code hard to read.  

    Secondly,  the login action is in the Menus Controller?  Why?  Just put the login in an Account Controller or Authentication Controller. 

    Saturday, September 7, 2019 12:14 PM
  • User696604810 posted

    thank you for reply

    i use Authuntication/login

    it work on controller menu

    and not work for controller Authuntication

    why not work for controller authuntication

    t

    Saturday, September 7, 2019 12:41 PM
  • User475983607 posted

    thank you for reply

    i use Authuntication/login

    it work on controller menu

    and not work for controller Authuntication

    why not work for controller authuntication

    t

    Again, your code is very very confusing.  Unfortunately, like your other threads the shared code does not compile.  Clearly the shared code is not the actual code that you are testing.  

    I built a simple test controller based in your code and it works just fine.  I feel it is confusing to route authentication/login to the menus controller but this is your code and can be as confusing as you like.  

        public class MenusController : Controller
        {
            public IActionResult Index()
            {
                return Ok("Index");
            }
    
            //GET: Security/UserPrograms/{userId}
            [HttpGet(Contracts.ApiRoutes.Security.GetUserMenus)]
            public IActionResult GetUserMenu(string userId)
            {
                return Ok(userId);
            }
    
            //POST: Authuntication/login
            [HttpPost(Contracts.ApiRoutes.Login.UserLogin)]
            public IActionResult PostUserLogins([FromBody] Users user)
            {
                return Ok(user.Username);
            }
        }
        public class Users
        {
            public string Username { get; set; }
        }

    Just be aware this design is very irritating because you force anyone trying to help you to read the code below to find the route.   Why make your code so complex?

        public class ApiRoutes
        {
            public const string SecurityBase = "Security";
            public static class Security
            {
                public const string GetUserMenus = SecurityBase + "/UserPrograms/{userId}";
            }
            //Api url For Login
            public const string SecurityLogin = "Authuntication";
            public static class Login
            {
                public const string UserLogin = SecurityLogin + "/login";
    
            }
        }

    Anyway, I configure the PostMan to POST to URL  https://localhost:44327/Authuntication/login.  With a body of...

    {
    	"Username": "Foo Bar"
    }

    .. and it just works.

    Saturday, September 7, 2019 1:34 PM