ADS MB
ADS MB

Reputation: 175

Using value of String property inside a [Route()] data annotation in Controller

I'm working on a ASP.NET MVC project, in a Action inside a Controller I try to use STG_Route.ADDproperty value inside [Route()] data annotation like this :[Route(STG_Route.ADD)] but Visual Studio show me this error :

An object reference is required for the non-static field, method, or property 'AdminController.STG_Route'

STG_Route is an object of a class, this class is STG_Route

STG_Route class code :

public class STG_Route:Routes
{
    public override string ADD => "/STG/Add";
    public override string SHOW => "/STG/Show";
    public override string PROFILE => "/STG/{CODE}";
}

Routes is another class

Please any help about how can I use value of a ADD property inside [Route()]

Thanks in advance.

Upvotes: 2

Views: 587

Answers (1)

MBARK T3STO
MBARK T3STO

Reputation: 339

An overriding property must be virtual, abstract and STG_Route inherits from Routes, ADD cannot be defined as a const string type, and then you cannot use STG_Route.ADD in Route[()]. I suggest that you do not let STG_Route inherit from Routes and assign values ​​directly to ADD.

STG_Route

public class STG_Route
{
    public const string ADD="/STG/Add";
    public const string SHOW= "/STG/Show";
    public const string PROFILE= "/STG/{CODE}";
}

Controller

   public class STGController : Controller
    {
        [Route(STG_Route.ADD)]
        public ActionResult ADD()
        {
            return View();
        }
    }

Upvotes: 1

Related Questions