在.NET API操作筛选器中重定向

时间:2017-07-06 21:09:31

标签: c# asp.net-web-api actionfilterattribute

我希望能够在ActionFilterAttribute中读取Request标头,并指导用户。我还想维护现有的请求,或将控制器和URL参数传递给新的请求。我知道这在MVC中很容易,但还没有在Web API中完成。

1 个答案:

答案 0 :(得分:4)

实际上这很容易。您只需创建HttpResponseMessage对象。

public class RedirectAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var response = actionContext.Request.CreateResponse(HttpStatusCode.Redirect);
            response.Headers.Location = new Uri("https://www.stackoverflow.com");
            actionContext.Response = response;
        }
    }
相关问题