4

I am writing an area for administering several subsites, almost all of the functionality will be the same across each site (add/edit/remove pages etc) and my repository on instantiation takes the SiteIdentity so all the data access methods are agnostic in relation to this. The problem I have at the moment is trying to make my action methods also agnostic.

The URL pattern I want to use is along the lines of:

"ExternalSite/{identity}/{controller}/{action}/{id}"

A naive approach is to have each action take the identity parameter, but this means having to pass this in to my repository on each action as well as include it in the ViewData for a couple of UI elements. I'd much rather have this something that happens once in the controller, such as in its constructor.

What is the best way to do this? Currently the best I can come up with is trying to find and cast identity from the RouteData dictionary but part of me feels like there should be a more elegant solution.

Chao
  • 3,033
  • 3
  • 30
  • 36

2 Answers2

1

You have access to your route values in Request.RequestContext.RouteData, so you can make base controller and public property SiteIdentity, in such case you can access it from all actions in all inherited controllers.

Giedrius
  • 8,430
  • 6
  • 50
  • 91
1

It sounds like you want to use OnActionExecuting or a Custom ModelBinder to do that logic each time you have a specific parameter name (also known as a RouteData dictionary key).

  1. Creating a custom modelbinder in ASP.NET MVC
  2. Creating an OnActionExecuting method in ASP.NET MVC, Doing Serverside tracking in ASP.NET MVC
Community
  • 1
  • 1
George Stocker
  • 57,289
  • 29
  • 176
  • 237
  • OnActionExecuting seems to be the best approach. – Chao Sep 28 '11 at 14:46
  • Just to further expand I ended up creating a method on the controller to set Identity related properties, Made an interface for this method and had the actionfilter call the method. This gives me the ability to have differing logic for differing controllers. – Chao Sep 29 '11 at 09:37