0

Hy, I'm using Attribute Routing for my project and I don't know how I can take the value of the parameter from the URL. I tried using the Request but I can't find it anywhere.

When I make GET: http://localhost:60163/courses/courseId=1 how can I take the value of 1 for courseId?

[RoutePrefix("courses")]    
public class CoursesController : ApiController
{
    [Route("{courseId}")]  //this is the value I need in the TeacherAuthenticationAttribute Action Filter
    [TeacherAuthorizationRequiered]
    public async Task<IHttpActionResult> GetCourse(int courseId=0)
    {
       Course course = await db.Courses.FindAsync(courseId);
        if (course == null)
            return NotFound();
        return Ok(course);
    }

In TeacherAuthorizationFilter I need the value of the courseId in order to validate it.

 public class TeacherAuthorizationRequieredAttribute : ActionFilterAttribute
 { 
  public override void OnActionExecuting(HttpActionContext filterContext)
      {
          if (filterContext.Request.Headers.Contains(Token))
             var courseIdValue = filterContext.Request.RequestUri.ParseQueryString()["courseId"];
      }
   }

Here is the problem, I don't know how I can get the value using the Request or if there is another way to do it. Thank you very much!

1 Answers1

0
public class TeacherAuthorizationRequieredAttribute : ActionFilterAttribute
{ 

  private const string Token = "Token";

  public override void OnActionExecuting(HttpActionContext filterContext)
  {
      if (filterContext.Request.Headers.Contains(Token))
         var courseIdValue =  filterContext.Request.RequestUri.ParseQueryString()["courseId"];

  }

}

now , you have to added in request body Token :"yourtoken"

Nazmul Hasan
  • 10,130
  • 7
  • 50
  • 73