Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Getting Route Values in Controller

Getting Route Values in Controller

Scheduled Pinned Locked Moved ASP.NET
databasequestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Farhad Eft
    wrote on last edited by
    #1

    Hi, I have the following Route Contex:

    context.MapRoute(
    "Space_default",
    "Space/{Value}/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional }
    );

    And I would like to get {Value} in my controller in the following code:

    public class OverviewController : Controller
    {
        string myValue = (int)RouteData.Values\["Value"\];
    
        \[CourseAccess(CourseId = myValue)\]
        public ActionResult Index(int CourseId)
        {...
    

    I was wondering how can I do that and use that value before my actions. Thank you.

    Richard DeemingR 1 Reply Last reply
    0
    • F Farhad Eft

      Hi, I have the following Route Contex:

      context.MapRoute(
      "Space_default",
      "Space/{Value}/{controller}/{action}/{id}",
      new { action = "Index", id = UrlParameter.Optional }
      );

      And I would like to get {Value} in my controller in the following code:

      public class OverviewController : Controller
      {
          string myValue = (int)RouteData.Values\["Value"\];
      
          \[CourseAccess(CourseId = myValue)\]
          public ActionResult Index(int CourseId)
          {...
      

      I was wondering how can I do that and use that value before my actions. Thank you.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You can't use a field or local variable in an attribute. The attribute parameters and properties are compiled into your code, so they can't depend on runtime data. You'll probably need to look at using a filter attribute: Understanding Action Filters (C#) | Microsoft Docs[^]

      public sealed class CourseAccessAttribute : FilterAttribute, IAuthorizationFilter
      {
      public void OnAuthorization(AuthorizationContext filterContext)
      {
      int courseId = (int)filterContext.RouteData["Value"];
      if (!HasAccessToCourse(filterContext, courseId))
      {
      filterContext.Result = new HttpUnauthorizedResult();
      }
      }

      private bool HasAccessToCourse(AuthorizationContext filterContext, int courseId)
      {
          ...
      }
      

      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      F 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        You can't use a field or local variable in an attribute. The attribute parameters and properties are compiled into your code, so they can't depend on runtime data. You'll probably need to look at using a filter attribute: Understanding Action Filters (C#) | Microsoft Docs[^]

        public sealed class CourseAccessAttribute : FilterAttribute, IAuthorizationFilter
        {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
        int courseId = (int)filterContext.RouteData["Value"];
        if (!HasAccessToCourse(filterContext, courseId))
        {
        filterContext.Result = new HttpUnauthorizedResult();
        }
        }

        private bool HasAccessToCourse(AuthorizationContext filterContext, int courseId)
        {
            ...
        }
        

        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        F Offline
        F Offline
        Farhad Eft
        wrote on last edited by
        #3

        Nice! Thank you! :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups