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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:49:42+00:00 2026-06-15T05:49:42+00:00

I have a MVC controller that loads a resource file and uses Server.MapPath to

  • 0

I have a MVC controller that loads a resource file and uses Server.MapPath to get the path to the file. I want to mock out the Server property in the controller object using Microsoft Fakes framework (I know how to do this using other frameworks).

Here’s the code:

    [HttpGet]
    public ActionResult GeneratePdf(string reportId)
    {
        var template = LoadTemplate(reportId);
        var document = pdfWriter.Write(GetReportModel(reportId), template);
        return File(document, MediaTypeNames.Application.Pdf);
    }

    private byte[] LoadTemplate(string reportId)
    {
        var templatePath = Server.MapPath(string.Format("~/ReportTemplates/{0}.docx", reportId));
        using(var templateContent = System.IO.File.OpenText(templatePath))
        {
            return Encoding.Default.GetBytes(templateContent.ReadToEnd());
        }
    }

The part I’m trying to mock out is the “Server.MapPath” method.

  • 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-15T05:49:43+00:00Added an answer on June 15, 2026 at 5:49 am

    As of Visual Studio 2012 Update 1, you can detour the Controller.Server property using Stubs.

    With the following .Fakes file in your test project:

    <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
      <Assembly Name="System.Web" Version="4.0.0.0"/>
      <StubGeneration>
        <Clear/>
        <Add FullName="System.Web.HttpContextBase!"/>
        <Add FullName="System.Web.HttpServerUtilityBase!"/>
      </StubGeneration>
      <ShimGeneration>
        <Clear/>
      </ShimGeneration>
    </Fakes>
    

    You can write the a unit test like this:

    [TestMethod]
    public void TestMethod1()
    {
        var target = new TestController();
    
        var serverStub = new StubHttpServerUtilityBase();
        serverStub.MapPathString = (path) => path.Replace("~", string.Empty).Replace("/", @"\");
    
        var contextStub = new StubHttpContextBase();
        contextStub.ServerGet = () => serverStub;
    
        target.ControllerContext = new ControllerContext();
        target.ControllerContext.HttpContext = contextStub;
    
        var result = (FilePathResult) target.Index();
        Assert.AreEqual(@"\Content\Test.txt", result.FileName);
    }
    

    With the upcoming Update 2, you will also be able to detour Controller.Server property directly using Shims. Here is the additional .Fakes file you will need with this approach.

    <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
      <Assembly Name="System.Web.Mvc" Version="4.0.0.0"/>
      <StubGeneration>
        <Clear/>
      </StubGeneration>
      <ShimGeneration>
        <Clear/>
        <Add FullName="System.Web.Mvc.Controller!"/>
      </ShimGeneration>
    </Fakes>
    

    And here is the test:

    [TestMethod]
    public void TestMethod2()
    {
        using (ShimsContext.Create())
        {
            var target = new TestController();
    
            var serverStub = new StubHttpServerUtilityBase();
            serverStub.MapPathString = (path) => path.Replace("~", string.Empty).Replace("/", @"\");
    
            var controllerShim = new ShimController(target);
            controllerShim.ServerGet = () => serverStub;
    
            var result = (FilePathResult)target.Index();
            Assert.AreEqual(@"\Content\Test.txt", result.FileName);
        }
    }
    

    Please note that this approach does not work in the current version (Update 1), due to limitations in Fakes runtime related to assemblies, like System.Web.Mvc that allow partially trusted callers. If you try to run the second test today, you will get a VerificationException.

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

Sidebar

Related Questions

I have a controller (Spring-MVC) that generates a PDF report on a get request.
I have the following Spring MVC controller that uses both a repository and an
I have the following getJSON that calls an MVC controller which returns inforamtion back:
I have an ASP.NET MVC 3 controller action. That action is defined as follows:
i have a quetion about MVC routing. I have a controller method that is
I have my own MVC and autoloader that loads classes. Everything works except that
I have a problem while returning jsp from controller using ajax+spring mvc. I want
I have an MVC application that is using MEF for the controllers, all working
I have 2 attributes that I use within the controllers of my MVC application.
I have an ASP.Net MVC Controller with a 'MapColumns' action along with a corresponding

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.