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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:39:46+00:00 2026-05-13T14:39:46+00:00

I have a controller action in ASP.NET MVC that handles uploaded files. However, it

  • 0

I have a controller action in ASP.NET MVC that handles uploaded files. However, it seems there is no way to call Request.Files.Count while using MvcContrib’s TestControllerBuilder.

I know I can work around this by abstracting Request.Files. My questions are:

  1. Is it indeed the case that there is no direct way to call Request.Files.Count when using the TestControllerBuilder? Or am I doing something wrong?
  2. Is there a way to stub the call to Request.Files.Count while using TestControllerBuilder using Rhino Mocks?
  3. Do you think I should submit a request or patch for handling Request.Files.Count to MvcContrib?

Example code:

I want to make sure that there is at least one file in the Request.Files collection so I have the following conditional in my action:

public class MyController : Controller {
    public ActionResult Upload() {
        if (Request.Files == null || Request.Files.Count == 0)
            ViewData.ModelState.AddModelError("File", "Please upload a file");
        // do stuff
        return View();
    }
}

I am using the TestControllerBuilder from MvcContrib to create the test double for my controller tests. However, the call to Request.Files.Count always seems to throw a an exception. For example running the following NUnit test throws a NotImplementedException during the call to controller.Upload() at the call to Request.Files.Count:

[Test]
public void Upload_should_return_default_view_given_one_file() {
    MyController controller = new MyController();
    TestControllerBuilder controllerBuilder = new TestControllerBuilder();
    controllerBuilder.InitializeController(controller);
    controllerBuilder.Files["file"] = 
        MockRepository.GenerateStub<HttpPostedFileBase>();

    var result = controller.Upload() as ViewResult;

    Assert.That(result.ViewData.ModelState.IsValid, Is.True);
    result.AssertViewRendered().ForView(string.Empty);
}

I’ve also attempted stubbing the call to Request.Files.Count to no avail (I’m using Rhino Mocks). None of the below work (even if I change controller and/or controllerBuilder to a stub):

controllerBuilder.Stub(cb => cb.HttpContext.Request.Files.Count).Return(1);
controller.Stub(c => c.Request.Files.Count).Return(1);

Thanks

  • 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-13T14:39:47+00:00Added an answer on May 13, 2026 at 2:39 pm

    I submitted a patch on Github to MvcContrib mantainers, but the changes are very simple to make, the problem is that HttpFileCollectionBase is not implementing Count() and this[int index] methods so they must be overriden by the WriteableHttpFileCollection in MvcContrib.

    I paste the code here for completeness, that must be added to WriteableHttpFileCollection class:

    public override HttpPostedFileBase this[int index]
    {
        get { return files[AllKeys[index]]; }
    }
    
    public override int Count
    {
        get { return files.Count; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.