Hy,我在项目中使用属性路由,但我不知道如何从URL中获取参数的值。我尝试使用请求但我无法在任何地方找到它。
当我进行GET时:http://localhost:60163/courses/courseId=1如何为courseId取值1?
[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);
}
在TeacherAuthorizationFilter中我需要courseId的值才能验证它。
public class TeacherAuthorizationRequieredAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext filterContext)
{
if (filterContext.Request.Headers.Contains(Token))
var courseIdValue = filterContext.Request.RequestUri.ParseQueryString()["courseId"];
}
}
这是问题所在,我不知道如何使用请求获取价值,或者是否有其他方法可以实现。非常感谢你!
答案 0 :(得分: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"];
}
}
现在,您必须在请求正文中添加 令牌:“yourtoken”