Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9105205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:02:55+00:00 2026-06-17T02:02:55+00:00

I have copied the following code from Wrox Professional ASP.NET 4.0 MVC 4 book,

  • 0

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”) with the little modification of making it protected and storing as utility method in my abstract application-wide Controller

protected ActionResult RedirectToLocal(string returnUrl)
{
    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RedirectToAction("Index", "Home");
    }
}

The code above is aimed at securing the MVC application from open redirection attacks, which are not subject of the question.

The code is obviously well-formed, compiles and I trust it works.

The problem arises when “smartly” changing the code above into the following one-liner

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

The one-liner above is supposed to do exactly the same as the extended code (no, ReSharper didn’t suggest me to replace, it was my initiative).

The compilation error is the following: there is no implicit conversion between System.Web.Mvc.RedirectResult and System.Web.Mvc.RedirectToRouteResult.

ReSharper then comes to help and suggests the following modification

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

The question is “why do I have to cast Redirect method”?

Since both Redirect and RedirectToAction return a subclass of ActionResult (verified via F12) and that subclass is the return value of the function, it should be automatically compatible. Or at least, in my knowledge of C#, either both codes compile or they both don’t.

To be more general, the question could be reformulated as follows:

Suppose I have class A, B and C

abstract class A {}
class B: A{}
class C: A{}

And suppose the following function works

private A Function(){
    if (condition) return new B();
    else return new C();
}

Why the following one-liner doesn’t compile?

private A Function(){
    return (condition) ? new B(): new C();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:02:56+00:00Added an answer on June 17, 2026 at 2:02 am

    It doesn’t compile because the compiler decides what the return-type of the one-liner is based upon one of the two return-values. Since type B doesn’t derive of type C, or the other way around, the two types are not mutually exclusive, and the return type of the if (as a whole) cannot be derived that way.

    If you would want to do it in a one-liner, you have to cast the return-values to the type that you want to return, which is type A.

    private A Function(){
        return (condition) ? ((A)new B()): ((A)new C());
    }
    

    UPDATE: I didn’t see that you already do the casting in your question, my apologies.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code, copied exactly from a exercise from a PHP book.
I have copied the code from the following example on the Apple Developer site.
I have a book called Network Programming in .NET which has the following code
I have the following code, copied from the Python manual: import logging LOG_FILENAME =
So I have the following code which I pretty much copied from here .
I have the following code which copies property values from one object to another
I have copied class from net which inherits UIImageView. How to put that class
I am learning MYSQL. I have copied this code from a learning website. The
I have put an UITableView inside a UIViewController and have copied code from UITableViewController
I have copied the C# code from Photobucket api and i am editing as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.