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 6804201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:26:13+00:00 2026-05-26T19:26:13+00:00

MVC 3. VB.NET I have the following controller action in my application to send

  • 0

MVC 3. VB.NET I have the following controller action in my application to send generated PDF files to the User by download. The function works fine when the controller itself has been invoked. But when I call to the function from outside the controller it returns a ” Object reference not set to an instance of an object ” error as soon as it hits the response.contenttype line. I have verified that all of the variables are in fact making it to the below action… But no dice if I try to call this in another controller:

   Dim _print as new PrintController
   _print.showUserPDF(firstName,lastName)
   return RedirectToAction("Registrants")

the function in the PrintController that is throwing the error is:

   Function showUserPDF(ByVal pdfName As String, ByVal fileName As String, ByVal _directory As String) As ActionResult
        If Not String.IsNullOrEmpty(pdfName) Then
            Response.ContentType = "application/pdf"
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName)
            Response.TransmitFile(pdfName)
            Response.[End]()

            Dim FileToDelete As String

            FileToDelete = pdfName

            If System.IO.File.Exists(FileToDelete) = True Then
                System.IO.File.Delete(FileToDelete)
            End If

            Directory.Delete(_directory)
            Return Nothing
        End If
        Return Nothing

    End Function

any ideas why this is only working when called explicitly from inside its containing controller???

  • 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-05-26T19:26:14+00:00Added an answer on May 26, 2026 at 7:26 pm

    Clearly there is no Response object set on the method when called from another controller, nor should there be if (a) it’s not being created by the MVC framework or (b) you aren’t explicitly populating the HttpContext property via the ControllerContext. This is really the wrong way to go about both downloading content and sharing code between two controllers.

    First, you should be using a FileResult, using the signature that takes a byte array since you might delete the file, to deliver a file as a download from a controller. The FileResult is explicitly designed to do this. You should not be writing directly to the Response from your controller as that violates the separation of concerns fundamental to MVC.

    Second, to share the code you should abstract the code to a shared base controller from which both derive or a helper class, preferably the former. That way you will have the method available directly in the controller created by the framework and won’t need to instantiate another controller just to invoke the method.

    Last, you can’t both deliver a file to download AND redirect in the same response. You might want to handle this client-side via javascript if this is required. Note the file download should leave you on the same page.

    I apologize in advance for any syntax or other errors. I read VB better than I write it

    Public Class ExtendedControllerBase
        Inherits Controller
    
        Function showUserPDF(ByVal pdfName As String, ByVal fileName As String, ByVal _directory As String) As ActionResult
                If Not String.IsNullOrEmpty(pdfName) Then
    
                    Dim input As New FileStream(pdfName, FileMode.Open)
                    Dim bytes(CInt(input.Length - 1)) As Byte
                    input.Read(bytes, 0, CInt(input.Length))
    
                    Dim FileToDelete As String
    
                    FileToDelete = pdfName
    
                    If System.IO.File.Exists(FileToDelete) = True Then
                        System.IO.File.Delete(FileToDelete)
                    End If
    
                    Directory.Delete(_directory)
                    Return File( bytes, "application/pdf", fileName )
                End If
                Return Content("") 'You might want to throw an exception here?
    
            End Function
    End Class
    
    Public Class PrintController
        Inherits ExtendedBaseController
    
        ...
    
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some doubts regarding the following controller action (in ASP.NET MVC, but it's
In an ASP.NET MVC application where I have the following model.. public class EventViewModel
In my ASP.NET MVC application, I have the following setup: <runtime> <assemblyBinding xmlns=urn:schemas-microsoft-com:asm.v1> <probing
ASP.Net MVC Controllers have several methods of forwarding control to another controller or action.
I have implemented custom errors in my asp.net mvc application by following this article
Hallo, I have following jquery code for calling ASP.NET MVC controller method that is
I have ASP.NET MVC application with action which should process posted XML data. At
I have an ASP.NET MVC application that produces a PDF file when calling an
I'm securing an ASP.NET MVC 2 application, and I have a user who is
In my ASP.NET MVC application I have the following GET input field: <% using

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.