0
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"];
}
}
這是問題所在,我不知道如何使用請求獲取值,或者如果有另一種方法來完成它。非常感謝你!