MVC Controller and URL logic?

Last post 04-21-2009 12:46 PM by punkracy. 4 replies.

Sort Posts:

  • Huh? [:^)] MVC Controller and URL logic?

    12-30-2008, 12:09 PM
    • Member
      5 point Member
    • ericis_com
    • Member since 05-03-2006, 9:01 PM
    • Phoenix, AZ
    • Posts 5

    (Begin Edit) Found the Answer: I do indeed have to mock the HttpContext per other's suggestions. There's just no effectively getting around it. And the "CarTrackr" application on CodePlex has a "MvcMockHelpers" static class to help. I thought this would be easier... (End Edit)


    I am setting the ASP.NET "Master Page" and current "Theme" using custom logic based on the URL and some database records. This logic started in my MVC View's OnPreInit, because it was obviously easy. But, I have multiple problems with this:

    • Logic to select a master template (or "view") should be in the Controller, not the View
    • I'm not seeing how to access the URL via the Controller
    • I'm also not seeing how I could pass a test URL to the Controller to run unit tests


    Any ideas out there?

  • Re: MVC Controller and URL logic?

    12-30-2008, 12:25 PM
    • Participant
      1,045 point Participant
    • srulyt
    • Member since 02-02-2008, 6:16 PM
    • Posts 226

    You can access Request.Url from your controller.

    To pass a URL you will have to mock the controllercontext

    for more info http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx

  • Indifferent [:|] Re: MVC Controller and URL logic?

    12-30-2008, 2:49 PM
    • Member
      5 point Member
    • ericis_com
    • Member since 05-03-2006, 9:01 PM
    • Phoenix, AZ
    • Posts 5

    Holy crapoly! I thought MVC would make my life easier! :D

    Simpler answer: I overload my controller's constructor to accept a URL and use the HttpContext's HttpRequest URL in the default constructor?

    Better simpler answer: I create a custom "MyController" base class with the overloaded constructor to accept a URL and inherit this new base class in all of my controllers.

    Mildly complex answer: Evolve my custom MyController's overloaded constructor to accept an "IApplicationContext" object whose actual implementation is injected by Unity and write custom implementations of "IApplicationContext" as needed that would likely mock HttpContext to some degree.

    Complex answer (IMHO): Mock the freaking HttpContext! Okay, it's probably not THAT bad.

    Filed under: , , ,
  • Re: MVC Controller and URL logic?

    04-21-2009, 11:40 AM
    • Member
      12 point Member
    • Seamus_m
    • Member since 04-20-2009, 3:43 PM
    • Posts 7

    I would use Typemock for this, it makes unit testing ASP.NET easier.

  • Re: MVC Controller and URL logic?

    04-21-2009, 12:46 PM
    • Member
      105 point Member
    • punkracy
    • Member since 02-05-2009, 11:39 AM
    • POZnan* | Poland
    • Posts 29

    ericis_com:

    Holy crapoly! I thought MVC would make my life easier! :D

     

    It has nothing to do with MVC. It's asp .net mvc "feature".  Look at rails, it works out of the box.

    Even with mocking HttpContext you will still have some problems with testing (view names, http return codes).

     Best regards

    Don't forget to click "Mark as Answer" on the post that helped you.
    http://punkracy.com
    http://tembo.pl
Page 1 of 1 (5 items)