1

I have a number of controllers that take a 'site' as a parameter within their actions, like so:

[AuthoriseSite]
public ActionResult Index(string site) {
    var model = gvmFactory.CreateAnIndexViewModel(site);
    return View("Index", model);
}

I have been passing the site as a parameter but I could also calculate it from a user's Membership profile, so my code could look like:

[AuthoriseSite]
public ActionResult Index() {
    string site = UserProfileService.GetUsersSite();   
    var model = gvmFactory.CreateAnIndexViewModel(sitename);
    return View("Index", model);
}

The site parameter can vary according to the role a user is in. Users that are admins can choose many different sites, whilst non-admins can only use the one they belong to.

My question is, which is the better practice?

I favour the first, it feels more like MVC, although this could just be I've always tended to do this. The second has some advantages in that URLs can not be adjusted so as non-admins get to see sites they shouldn't. However, the filter [AuthoriseSite] prevents this.

1

If its common to many controllers and you use it in a lot of places, sounds like you need a baseController that your controllers can inherit from, and then put that logic there.

If you do that, you don't need to pass unnecessary arguments to your actions, and your actions don't need the same code copy pasted everywhere.

  • I'd been putting off creating a base controller for a while, your comment finally made me go and create it. Thanks. – Daniel Hollinrake Jul 15 '14 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.