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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:00:17+00:00 2026-06-03T12:00:17+00:00

I’m writing specflow tests using Watin, for an Asp.Net MVC application which uses T4MVC.

  • 0

I’m writing specflow tests using Watin, for an Asp.Net MVC application which uses T4MVC.

I find myself using “magic string” urls in the tests, which I don’t like.

[Given(@"I am on the sign up page")]
public void GivenIAmOnTheSignUpPage()
{
    string rootUrl = ConfigurationManager.AppSettings["RootUrl"];
    string fullUrl = string.Format("{0}/Authentication/Signup",rootUrl);
    WebBrowser.Current.GoTo(fullUrl);
}

I would much rather use my T4MVC Action Results like I do in the MVC App, something like this…

[Given(@"I am on the sign up page")]
public void GivenIAmOnTheSignUpPage()
{
    WebBrowser.Current.GoTo(MVC.Authentication.SignUp().ToAbsoluteUrl());
 }

My ToAbsoluteUrl Extension Method

public static class RouteHelper
{
    private static UrlHelper _urlHelper;
    private static string _rootUrl;

    public static string ToAbsoluteUrl(this ActionResult result)
    {
        EnsureUrlHelperInitialized();

        var relativeUrl = _urlHelper.Action(result);
        return string.Format("{0}/{1}", _rootUrl, relativeUrl);
    }

    private static void EnsureUrlHelperInitialized()
    {
        if (_urlHelper==null)
        {
            _rootUrl = ConfigurationManager.AppSettings["RootUrl"];

            var request = new HttpRequest("/", _rootUrl, "");
            var response = new HttpResponse(new StringWriter());
            var context = new HttpContext(request,response);
            HttpContext.Current = context;
            var httpContextBase = new HttpContextWrapper(context);


            RouteTable.Routes.Clear();
            MvcApplication.RegisterRoutes(RouteTable.Routes);

            var requestContext = new RequestContext(httpContextBase, RouteTable.Routes.GetRouteData(httpContextBase));

            _urlHelper = new UrlHelper(requestContext, RouteTable.Routes);
        }
    }
}

What is the correct way to initialize the RequestContext and RouteCollection so that I can generate my test URLs?

Currently I receive a NullReferenceException on the line var requestContext = new RequestContext(httpContextBase, RouteTable.Routes.GetRouteData(httpContextBase));. Is that the right way to new up a requestContext?

Or if there is a better way to take an ActionResult (from T4MVC) and resolve it to an absolute url, outside of a web app, that’s really what I’m looking for.

  • 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-03T12:00:30+00:00Added an answer on June 3, 2026 at 12:00 pm
    public static class RouteHelper
    {
        private static UrlHelper _urlHelper;
        private static string _rootUrl;
    
        static RouteHelper()
        {
            var routes = new RouteCollection();
            MvcApplication.RegisterRoutes(routes);
            var req = new HttpRequest(string.Empty, "http://www.site.com", null);
            var res = new HttpResponse(null);
            var ctx = new HttpContext(req, res); // do not use HttpContext.Current
            var requestContext = new RequestContext(new HttpContextWrapper(ctx), 
                new RouteData());
            _urlHelper = new UrlHelper(requestContext, routes);
            _rootUrl = ConfigurationManager.AppSettings["RootUrl"];
        }
    
        public static string ToAbsoluteUrl(this ActionResult result)
        {
            return string.Format("{0}{1}", _rootUrl, _urlHelper.Action(result));
        }
    }
    

    The static constructor sets up your private fields. I chose to use a new RouteCollection, instead of using the static RouteTable.Routes property, but you might be able to.

    I don’t think the constructors for the HttpRequest and HttpResponse matter. I just passed in some strings to get them to construct without throwing an exception. Use those to construct a brand new HttpContext (don’t use HttpContext.Current when running from xUnit). You can then put it into an HttpContextWrapper to get your HttpContextBase reference.

    Construct a new RequestContext, passing in your base wrapper and a new RouteData instance. Use that, along with your previous RouteCollection to construct the UrlHelper. Note that its Action method will return strings prepended with “/”, so you should leave that out of our RootUrl appSetting (so use something like value=”https://develop.site.com” without the trailing slash).

    Note this will not work for routes defined in MVC areas. For that, you need to register the areas in addition to calling RegisterRoutes in global asax.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.