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

  • Question

  • User1236783433 posted

    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.

    Tuesday, June 9, 2020 5:19 PM

Answers

  • User1686398519 posted

    Hi,  MBARK

    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.

    This link can better help you understand the Route attribute.

    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();
            }
        }

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 10, 2020 9:10 AM

All replies

  • User-474980206 posted

    c# attributes only support static values as they are resolved at compile. try:

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

    [Route(STG_Route.ADD)]

    of course, static fields can not be overridden

    Tuesday, June 9, 2020 6:55 PM
  • User1236783433 posted

    This doesn't help, thanks for your time bro

    Tuesday, June 9, 2020 7:10 PM
  • User1686398519 posted

    Hi,  MBARK

    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.

    This link can better help you understand the Route attribute.

    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();
            }
        }

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 10, 2020 9:10 AM
  • User1236783433 posted

    Massive thanks bro, but I got this error 

    The route template separator character '/' cannot appear consecutively. It must be separated by either a parameter or a literal value.
    Parameter name: routeTemplate

    Please any help ?

    Thursday, June 11, 2020 8:22 AM
  • User1236783433 posted

    Sorry it's works fine massive thanks bro

    Thursday, June 11, 2020 9:13 AM