开发者

Limiting Features in an asp.net mvc application?

开发者 https://www.devze.com 2023-02-19 04:46 出处:网络
I am wondering what is the best way of limiting features for accounts. Say I have a free account and a premium account. How would I limit features off that only the premium account would get?

I am wondering what is the best way of limiting features for accounts.

Say I have a free account and a premium account. How would I limit features off that only the premium account would get?

Right now the only thing I can think of is lots of "if statements" in the view.

Something of the items that I need to limit off are.

  1. Jquery Ui Tabs(Premium account have one extra jquery ui tab - free has 4 but premium has 5).

So right now this would mean I need to if statements on for the unordered list to hide and one for the div that contains it. Maybe one if I am using ajax tabs.

  1. hyper links. I have some hyper links that load up jquery dialogs that need to be hidden from free users again the only thing I can think of is an if statement.

These are the kind of things that I am anticipating that I need to hide from free accounts.

I think it is going to look nasty in the view with all these "if statements" however I don't want to have multiple views(one view for free account and one for premium) as that going to mean that I might have to start repeating code,(even with the use of partial views) gets more confusing as now I have more files to look through, more a开发者_JS百科ction results to create and determine which to use.

On one site I tried multiple views for account and action results and it did not work out to well.

So what are the best ways to go about it?


First off you'll want to add this logic to the controller, not the view. You'll send some data to the view that signals to it that it should show the premium vs. standard features. But the decisions on whether your in a premium or a standard state should be made in the controller.

So your views may have if statements, or some sort of logic to decide whether to show certain features or not, but they'll be based on this "accountlevel" data that you send from your controller.

If you're putting an "if" statement into a view, or many as in your case, you ought to think about writing an HTML helper instead. That would clean up your code considerably. Instead of writing multiple if's, you'd have something like HtmlHelper.JQueryTabs() in your view. In the JQueryTabs method you'd have all your logic to output the appropriate HTML/Javascript depending on the Account Level.

Here's an article on creating custom HTML helpers


You could use a view model with two collections, one for links and one for tabs. The controller would populate these, based on the users' permissions. In the view, you could iterate round these collections and display them accordingly.


I would approach this by creating roles under the membership provider. Then you can decorate either your entire controller or actions within it along the lines of:

[Authorize(Roles = "Premium")]
public virtual ActionResult ShowPremiumContent()
{
    return View();
}

there are some good resources online that give different approaches to the same end game:

Restrict Area to a given role

http://weblogs.asp.net/srkirkland/archive/2010/01/04/authorizing-access-via-attributes-in-asp-net-mvc-without-magic-strings.aspx

http://geekswithblogs.net/thomasthedeuce/archive/2009/06/25/133056.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消