views:

248

answers:

1

I need to have some navigation options, that require keys that are specific to the current user, that reside in a masterpage. I need some advice on best practise.

In have the following links in a left nav in a masterpage

http://www.example.com/manageShop/123

http://www.example.com/addProductToShop/123

http://www.example.com/addStaffToShop/123

Where '123' is the shop id that the current user is the manager of. I need some way of passing this to the masterpage

Currently I'm going something to this effect:

<li><%= Html.ActionLink<ShopController>(x => x.ManageShop((int)Session["ShopKey"]), "Manage")%></li>

I thought this was a good idea as I only have to set the ShopKey once in the session and its done, the down side is that iv noticed that the session gets mixed if you have the site open is two tabs.

Alternatively I tried this:

<li><%= Html.ActionLink<ShopController>(x => x.ManageShop((int)ViewData["ShopKey"]), "Manage")%></li>

But this means you have to keep setting the ViewData in every action in every controller. Which is awful.

EDIT: I have had alook at filters like eu-ge-ne suggested below, but I dont this really solves my problem as I still have the issue of setting the ShopKey everywhere?

What is the solution?

+2  A: 

You can create custom filter for this:

public class UserKeyAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData["UserKey"] = UserKey;
    }
}

and use it on your controller or controller actions

[UserKey]
public class YourController : Controller
{

// or

public class YourController : Controller
{
    [UserKey]
    public ActionResult Index()
    {

or use Controller.OnActionExecuting() (or even create base controller for this as Arnis L. said):

public class BaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData["UserKey"] = UserKey;
    }
}

// and then derive your controllers from BaseController

public class YourController : BaseController
{
eu-ge-ne
So in this line : filterContext.Controller.ViewData["UserKey"] = UserKey; What is setting the value of UserKey? Are you no just moving the problem rather than solving it?
Dan
Dan, my answer was only about "setting the ViewData in every action in every controller. Which is awful". Could you post the code from your question how did you set Session["UserKey"]. What is http://www.example.com/manageprofile/123 ? Is it link for current user (in that case you can use something simpler - for example http://www.example.com/manageprofile)? Or current user is Admin and there are several links to manage profiles of usual users on master page?
eu-ge-ne
Eugene Im just setting Session["UserKey"] in one of my controller actions (Its just in one place, where its logical to be set, I don't think a code snippet would illustrate anything). My example is contrived to illustrate a point - but i defiantly need the keys on the end.
Dan