2
[HttpGet]
public ActionResult Login(string? returnUrl)
{
    if (Request.IsAuthenticated)
    {
        if(returnUrl.HasValue)
           return RedirectToAction("Index", "Home");
        else
           return RedirectToAction(returnUrl);
    }
    return View();
}

enter image description here

Error: The best overloaded method match for 'System.Web.Mbv.Controller.Redirect(string)' has some invalid arguments

How can use nullable string for RedirectToAction()

2
  • 5
    string is already nullable. string? doesn't make sense. What are you try to do exactly? Commented Nov 25, 2015 at 11:47
  • 1
    string is a reference type in c# Commented Nov 25, 2015 at 11:55

1 Answer 1

7

String is nullable already but you can check for null with string.IsNullOrEmpty.

[HttpGet]
public ActionResult Login(string returnUrl)
{
        if (Request.IsAuthenticated)
        {
           if(string.IsNullOrEmpty(returnUrl))
           {
               return RedirectToAction("Index", "Home");
           }
           else
           {
               return RedirectToAction(returnUrl);
           }
        }
        return View();
}

You could also default it so if it's not passed in it will never be empty.

[HttpGet]
    public ActionResult Login(string returnUrl = "www.yourDomain.com/login")
    {
            if (Request.IsAuthenticated)
            {
               return RedirectToAction(returnUrl);
            }
            return View();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.