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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:17:28+00:00 2026-05-23T12:17:28+00:00

I am testing code in a MVC HTML helper that throws an error when

  • 0

I am testing code in a MVC HTML helper that throws an error when trying to get the application path:

//appropriate code that uses System.IO.Path to get directory that results in:
string path = "~\\Views\\directory\\subdirectory\\fileName.cshtml";
htmlHelper.Partial(path, model, viewData); //exception thrown here

The exception that is thrown is

System.Web.HttpException: The application relative virtual path ‘~/Views/directory/subdirectory/fileName.cshtml’ cannot be made absolute, because the path to the application is not known.

Following the advice of How to resolve issue with image path when testing HtmlHelper?
I have faked (using Moq):

  • Request.Url to return a string
  • Request.RawUrl to return a string
  • Request.ApplicationPath to return a string
  • Request.ServerVariables to return a null NameValueCollection
  • Response.ApplyAppPathModifier(string virtualPath) to return a string

What else is needed to be able to allow this code to run in the context of a unit test run?
Or
What other approach should I be taking to render a Partial view on a dynamically built string?

  • 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-23T12:17:29+00:00Added an answer on May 23, 2026 at 12:17 pm

    As an alternative to mocking built-in .net classes, you can

    public interface IPathProvider
    {
        string GetAbsolutePath(string path);
    }
    
    public class PathProvider : IPathProvider
    {
        private readonly HttpServerUtilityBase _server;
    
        public PathProvider(HttpServerUtilityBase server)
        {
            _server = server;
        }
    
        public string GetAbsolutePath(string path)
        {
            return _server.MapPath(path);
        }
    }
    

    Use the above class to get absolute paths.

    And for For unit testing you can mock and inject an implementation of IPathProvider that would work in the unit testing environment.

    –UPDATED CODE

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

Sidebar

Related Questions

How are people unit testing code that uses Linq to SQL?
I am interested in your opinions on unit-testing code that uses Corba to communicate
I get this error, while I'm testing the code below: You have an error
I'm trying to add unit testing to an ASP.NET MVC application I have built.
I've been working on an ASP.net MVC project that uses checkbox HTML Helpers in
While reading an Asp.Net MVC code sample that used MbUnit as it's testing framework,
I'm trying to be better about unit testing my code, but right now I'm
I have been trying to learn how to add testing to existing code --
In an Asp.net MVC application I'd like to encapsulate the ugly wrapper code (just
For some testing code, I'd like to be able to host a WCF service

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.