3

I have a number of permissions, and based on a set of conditions these permission determine if a user can see certain features. I have written a helper function for this as the logic in the view became quite extensive.

Essentially I'm looking for a function the same as Html.ActionLink that I can access from a class file (Ideally if I can access the Helper that would be great) So I can do somthing like so,

public static string GetAdminLinks()
{
    if(PermCheck)
    {
        return(Html.ActionLink(...));
    }
}

Any sugestions?

0

2 Answers 2

6

in controller:

Url.Action("Index", "Home", null, Request.Url.Scheme); 
Sign up to request clarification or add additional context in comments.

Comments

3

It largely depends on how your permission check is implemented (and of which information it needs to determine the user's permissions). Anyhow, I'd implement it as an extension to the HtmlHelper class.

Somewhere in your App_Code:

using System.Web.Mvc.Html;

public static class HtmlHelperExtensions {
    public static string SecureActionLink(this HtmlHelper htmlHelper, string action, string controller){
        if(PermCheck)
            return htmlHelper.ActionLink(action, controller);
        else
            return string.Empty;
    }

    //add other ActionLink overrides if you like...
}

Then you'll be able to call the extension method from anywhere in your ViewPages without any code behind.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.