C# 自定义操作筛选器中的重定向
我正在asp.netC# 自定义操作筛选器中的重定向,c#,asp.net-mvc,C#,Asp.net Mvc,我正在asp.netMVC 5中创建自定义筛选器,并尝试在执行操作时重定向到方法中的特定控制器我尝试了重定向到操作,但没有任何建议? 我正在web api控制器中使用此过滤器 这是我的密码: public override void OnActionExecuting(HttpActionContext actionContext) { Uri MyUrl = actionContext.Request.RequestUri; var host = MyUrl.Host;
MVC 5
中创建自定义筛选器,并尝试在执行操作时重定向到方法中的特定控制器
我尝试了重定向到操作
,但没有任何建议?
我正在web api控制器中使用此过滤器
这是我的密码:
public override void OnActionExecuting(HttpActionContext actionContext)
{
Uri MyUrl = actionContext.Request.RequestUri;
var host = MyUrl.Host;
if (host == "localhost")
{
// redirect should be here
}
}
如果您使用的是
MVC 5.2.3
,您的操作过滤器应该如下所示
public class CustomActionFilter : ActionFilterAttribute, IActionFilter
{
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
{
}
}
filterContext.Result =
new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "ControllerName" },
{ "action", "Action" }
});
要重定向到操作,可以使用如下代码
public class CustomActionFilter : ActionFilterAttribute, IActionFilter
{
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
{
}
}
filterContext.Result =
new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "ControllerName" },
{ "action", "Action" }
});
对于WebApi,您可以使用:
代码在哪里?
它不工作
这是什么意思?您需要共享代码,并告诉我们在哪里使用自定义过滤器。你调试代码了吗?你能调试自定义过滤器代码吗?我更新了我的问题,并添加了代码,可能与你使用的MVC版本重复?