1

I have the following code in controller:

public ActionResult Download(string file){
    ...
    ...

    if(System.IO.File.Exists(file)){
        return File(File.Open(file, FileMode.Open, FileAccess.Read), contentType, newFilename);
    }

    return ??;  //what should return here ?
}

If no file exists then stay on same page where that action was called. How to do that ?

If return null then will display a blank page.

1 Answer 1

1

You can do that by taking a look at this question ActionResult return to page that called it. The first answer by Chuck gives you all that you need:

return Redirect(HttpContext.Request.UrlReferrer.AbsoluteUri);
Sign up to request clarification or add additional context in comments.

2 Comments

It give me error: Cannot convert expression RedirectResult to FileResult
@SnakeEyes is your method truly ActionResult Download or is it FileResult Download? The reason I ask is because Redirect produces a RedirectResult which implements ActionResult. FileResult also implements ActionResult. If you keep the method return type ActionResult you shouldn't be having that issue

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.