views:

45

answers:

1

Hi folks,

I have a simple class that contains some general information about the current web site :-

public class WebSiteSettings
{
    public EnvironmentType Environment { get; set } // Eg. Production
    public VersionType Version { get; set; } // Eg. Version2
    public string ApiKey { get; set; } // Eg. ABCDE-1234
}

Now, I wish to have the following Route :-

// Api - Search methods.
routes.MapRoute(
    "Search Methods",
    "{versionDate}/{controller}/{action}"
);

and here's a sample url:

http://localhost.api.mydomain.com:1234/2010-01-23/search/posts?user=JonSkeet&apikey=ABCDE-1234

I currently handle retrieving the apikey via a custom ActionFilter. That's kewl. But i'm not sure how i can extract the versiondate (ie. 2010-01-23) from the url and populate that simple class i made .. which all controllers will have access too.

I was initially thinking of doing this:

public abstract class AbstractController : Controller
{
    protected WebSiteSettings WebSiteSettings { get; set; }

    protected AbstractController() : base()
    {
        WebSiteSettings = new WebSiteSettings();
        // 1. Read in the version data and figure out the version number.
        // 2. Read in the app settings to figure out the environment. (easy to do).
        // 3. No api key just yet. this will be handled in the OnAuthorization action filter.
    }
}

public class SearchController : AbstractController
{
    public ActionResult Posts(..)
    { // ... blah ... }
}

So, i tried this and the RouteData was null, in the abstract controller. In fact, most of the information was null, in the abstract controller.

So i'm not sure when the proper place is to read in those values and create that class.

Anyone have an idea?

+2  A: 

You might want to try doing that stuff in the AbstractController.OnActionExecuting method, the constructor is too early.

protected override void OnActionExecuting(ActionExecutingContext context)
{
    // context has all the goods
}

http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onactionexecuting.aspx?ppud=4

jayrdub
Yep, that looks like an appropriate place to me. +1
Odd
Yep. Confirmed. Didn't know u could do dat. Sauce of awesome.
Pure.Krome