0

I'm trying to create a custom attribute to validate a session state in every action method of my MVC 5 app.

This is the code of the custom attribute.

[AttributeUsage(AttributeTargets.Method)]
public class CheckSession : ActionFilterAttribute
{
    public string SessionKey { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionParameters.ContainsKey(SessionKey))
        {
            string value = filterContext.ActionParameters[SessionKey] as string;

            if ((string)filterContext.HttpContext.Session[value] == null)
            {
                var control = filterContext.Controller as Controller;

                if (control != null)
                {
                    filterContext.Result = new RedirectToRouteResult(
                        new System.Web.Routing.RouteValueDictionary
                        {
                            {"controller", "Home"},
                            {"action", "Error"}, 
                            {"area", ""}
                        }
                    );
                }
            }
            else
            {
                base.OnActionExecuting(filterContext);
            }
        }
    }
}

The constants for the session keys that I'm using:

public static class SessionKeysConstants
{
    public static readonly string SMSNotificationsSearchClient = "SMSNotificationsSearchClient";
}

And I'm using the custom attribute like this:

[CheckSession(SessionKey = SessionKeysConstants.SMSNotificationsSearchClient)]
public ActionResult Index()
{
    // You need a session to enter here!
    return View("Index");
}

And getting the following error:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

I don't understand why, I'm using a constant and only works in assign a value string directly to the SessionKey parameter.

5
  • 3
    It would need to be public const string SMSNotificationsSearchClient = "SMSNotificationsSearchClient"; (static does not mean constant) Commented Oct 14, 2016 at 21:42
  • It's not working, I'm getting the same error. Commented Oct 14, 2016 at 21:48
  • Then you did not use the code above (I have just tested it and its fine) Commented Oct 14, 2016 at 21:50
  • I thought it was public readonly string SMSNotificationsSearchClient = "SMSNotificationsSearchClient";, without the static keyword, did you edited the text? Commented Oct 14, 2016 at 21:52
  • An yeah, it really works! Commented Oct 14, 2016 at 21:52

1 Answer 1

2

Attribute parameters are restricted to constant values of the following types:

  • Simple types (bool, byte, char, short, int, long, float, and double)
  • string
  • System.Type
  • enums
  • object (The argument to an attribute parameter of type object must be a constant value of one of the above types.)
  • One-dimensional arrays of any of the above types

Reference: https://msdn.microsoft.com/en-us/library/aa288454(v=vs.71).aspx

You can resolve your problem if you define SessionKeysConstants as an enum. And for that enum, one named constant is SMSNotificationsSearchClient.

As @StephenMuecke said above you can also make your string const too.

I would prefer an enum, this is somehow a standard if you're looking to data annotations(for example): https://msdn.microsoft.com/en-us/library/dd901590(VS.95).aspx

Basically your SessionKeysConstants is an enumeration of named constants, which by definition is an enum, but this is just my personal opinion.

Sign up to request clarification or add additional context in comments.

1 Comment

It's not a constant one.

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.