필터를 다른 작업으로 리디렉션하는 방법은 무엇입니까?
RedirectToAction
보호되며 우리는 액션 내부에서만 사용할 수 있습니다. 하지만 필터에서 리디렉션하려면?
public class IsGuestAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!Ctx.User.IsGuest)
filterContext.Result = (filterContext.Controller as Controller)
.RedirectToAction("Index", "Home");
}
}
RedirectToAction
는를 생성하는 도우미 메서드 일 뿐이 RedirectToRouteResult()
므로 작업에 대한 값 RedirectToRouteResult()
과 함께 새로운 전달을 생성하기 만하면 RouteValueDictionary()
됩니다.
아래 주석에 @Domenic의 코드를 기반으로 한 완전한 샘플 :
public class IsGuestAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!Ctx.User.IsGuest)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "Home" },
{ "action", "Index" }
});
}
}
}
다음은 코드 예입니다.
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!Ctx.User.IsGuest)
{
RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("action", "Index");
redirectTargetDictionary.Add("controller", "Home");
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
}
나는 파티에 조금 늦었다는 것을 알고 있지만 veggerby의 솔루션을 사용하고 일부 사람들에게 유용 할 수있는 도우미 클래스를 만들었으므로 여기에 제공하고 싶었습니다.
public static class ActionFilterHelpers
{
public static void Redirect(this ActionExecutingContext filterContext, String controller, String action, object routeValues)
{
filterContext.Result = Redirect(controller, action, routeValues);
}
public static ActionResult Redirect(String controller, String action, object routeValues)
{
var routeValues = new RouteValueDictionary();
routeValues["controller"] = controller;
routeValues["action"] = action;
foreach (var keyValue in new ObjectDictionary(routeValues))
routeValues.Add(keyValue.Key, keyValue.Value);
return new RedirectToRouteResult(routeValues);
}
}
리디렉션을 반환하는 정적 메서드 ActionResult
와 확장하는 확장 메서드를 모두 제공했습니다 filterContext
. 누군가 이것이 유용하다고 생각하기를 바랍니다.
ObjectDictionary
is a class that uses reflection to create a dictionary from the properties of the object from which it is constructed. I didn't include that code because I believe there is a better way to do that somewhere in the framework. I haven't found it yet, but I don't want others to inherit my potential bugs.
Security/Authorization/Authentication Filters should use the AuthorizeAttribute and IAuthorizationFilter.
public class IsGuestAttribute: AuthorizeAttribute, IAuthorizationFilter
{
public void OnResultExecuted(ResultExecutedContext filterContext)
{
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!Ctx.User.IsGuest)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "Home" },
{ "action", "Index" }
});
}
}
}
Usage: filterContext.RedirectToAction("Login", "Account");
Here's a helper class I wrote with some extension methods written to provide RedirectToAction functionality in more places. This is far too late for the OP but hopefully it helps someone!
public static class RedirectHelper
{
// RedirectToAction Extension Methods
public static void RedirectToAction(this HttpResponseBase response, String action, String controller, object routeValues = null, bool endResponse = false)
{
response.RedirectToRoute(CreateRoute(action, controller, routeValues));
if (endResponse) response.End();
}
public static void RedirectToAction(this HttpResponse response, String action, String controller, object routeValues = null, bool endResponse = false)
{
response.RedirectToRoute(CreateRoute(action, controller, routeValues));
if (endResponse) response.End();
}
public static void RedirectToAction(this ActionExecutingContext filterContext, String action, String controller, object routeValues = null, bool endResponse = false)
{
if (endResponse) filterContext.HttpContext.Response.RedirectToAction(action, controller, routeValues, true);
else filterContext.Result = new RedirectToRouteResult(CreateRoute(action, controller, routeValues));
}
public static void RedirectToAction(this ExceptionContext filterContext, String action, String controller, object routeValues = null, bool endResponse = false)
{
if (endResponse) filterContext.HttpContext.Response.RedirectToAction(action, controller, routeValues, true);
else {
filterContext.ExceptionHandled = true;
filterContext.Result = new RedirectToRouteResult(CreateRoute(action, controller, routeValues));
}
}
// Route Value Derivation
public static RouteValueDictionary CreateRoute(String action, String controller, object routeValues = null)
{
RouteValueDictionary result = routeValues != null ?
HtmlHelper.AnonymousObjectToHtmlAttributes(routeValues) :
new RouteValueDictionary();
result["controller"] = controller;
result["action"] = action;
return result;
}
}
There are more ControllerContexts that are not included but it should be fairly easy to add your own based on your needs.
참고URL : https://stackoverflow.com/questions/550995/how-to-get-filter-to-redirect-to-another-action
'IT TIP' 카테고리의 다른 글
AngularJS로 옵션 선택에서 빈 옵션 제거 (0) | 2020.10.15 |
---|---|
Cordova config.xml을 통해 iOS .plist 파일에 항목 추가 (0) | 2020.10.15 |
Vim 삽입 모드에서 파일 경로 자동 완성을 추가하는 방법이 있습니까? (0) | 2020.10.15 |
Spring MVC 뷰 레이어를위한 JSP의 대안 (0) | 2020.10.15 |
PHP-부동 숫자 정밀도 (0) | 2020.10.15 |