简体   繁体   中英

.NET-Core 3 MVC Controller Url.Action returning null

I have the following functions in my controller. I am trying to have the "MethodAsync" create a callback URL to the "CallbackAsync" method.

For thirdPartyCallbackUrl 0 to 3, I get a correct URL. However, for 4 to 7, I get a null value. I can't figure out what is going on.

    [Route("auth")]
    [ApiController]
    public class AuthController : Controller
    {
        [HttpGet("othermethod")]
        public async Task<IActionResult> OtherMethod()
        {
            return Ok();
        }

        [HttpGet("third-party-callback")]
        public async Task<IActionResult> CallbackAsync()
        {
            return Ok();
        }

        [HttpPost("third-party-login/{loginType}")]
        public async Task<IActionResult> MethodAsync()
        {
            string thirdPartyCallbackUrl = Url.ActionLink(action: nameof(CallbackAsync));
            string thirdPartyCallbackUrl1 = Url.ActionLink(action: nameof(CallbackAsync), controller: "Auth");
            string thirdPartyCallbackUrl2 = Url.ActionLink(action: "CallbackAsync", controller: "Auth");
            string thirdPartyCallbackUrl3 = Url.ActionLink(action: "CallbackAsync");
            string thirdPartyCallbackUr4 = Url.ActionLink(action: nameof(OtherMethod));
            string thirdPartyCallbackUrl5 = Url.ActionLink(action: nameof(OtherMethod), controller: "Auth");
            string thirdPartyCallbackUrl6 = Url.ActionLink(action: "OtherMethod", controller: "Auth");
            string thirdPartyCallbackUrl7 = Url.ActionLink(action: "OtherMethod");

            ...
        }
    }

Turns out it has an issue with the "Async" suffix on my "CallbackAsync" method. As soon as I dropped that, everything was working fine.

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