使用属性路由从请求中获取参数
Take parameter from request with attribute routing
嗨,我正在为我的项目使用属性路由,但我不知道如何从 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"];
}
}
问题出在这里,我不知道如何使用 Request 获取值,或者是否有其他方法可以做到这一点。非常感谢你!
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"
嗨,我正在为我的项目使用属性路由,但我不知道如何从 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"];
}
}
问题出在这里,我不知道如何使用 Request 获取值,或者是否有其他方法可以做到这一点。非常感谢你!
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"