Sending a parameter to a controller in ASP MVC 2

I am writing an area for administering several subsites, almost all functions will be the same on each site (add / edit / delete pages, etc.), and my repository accepts SiteIdentity when creating an instance, therefore all methods of accessing data are agnostic due to by this. The problem that I have at the moment is trying to make my methods of action also agnostic.

The URL pattern I want to use matches the lines:

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

The naive approach is for each action to take an identification parameter, but this means that you need to transfer it to my repository for each action, and also include it in the ViewData for several user interface elements. I would prefer this to happen in the controller, for example, in its constructor.

What is the best way to do this? For now, the best I can think of is to find and give out an identity from the RouteData dictionary, but part of me seems to be a more elegant solution.

+4
source share
2 answers

It looks like you want to use OnActionExecuting or Custom ModelBinder to execute this logic every time you have a specific parameter name (also known as a RouteData dictionary key).

+1
source

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

+1
source

All Articles