Understanding C# compilation error with ternary operator

前端 未结 2 1686
礼貌的吻别
礼貌的吻别 2020-12-19 19:04

I have copied the following code from Wrox Professional ASP.NET 4.0 MVC 4 book, page 179 (Chapter \"Understanding the Security Vectors in a Web Application\") w

2条回答
  •  心在旅途
    2020-12-19 19:39

    The compiler expects the same return type in a conditional statement, so it expects both of these to be the same type. If you cast the options to ActionResult, it will work. You may be able to get along with the compiler by only casting the last one.

    return (Url.IsLocalUrl(returnUrl)) ? (ActionResult)Redirect(returnUrl) : (ActionResult)RedirectToAction("Index", "Home");
    

提交回复
热议问题