[英]Redirect to a webpage using an attribute in Asp.net webforms with web.api
I created this attribute class to redirect to a web page url: 我创建了此属性类以重定向到网页URL:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using UtilityLibrary;
namespace xxxxxxx.Web.Attributes
{
public class SessionExpireWebApiFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
public string UrlPortal { get { return System.Configuration.ConfigurationManager.AppSettings["Portal"].ToString(); } }
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext filterContext)
{
try
{
if (StateManager.Instance.Get(Key.Autenticacao, State.Session) == null)
{
filterContext.Response = new HttpResponseMessage(HttpStatusCode.RedirectMethod);
filterContext.Response.Headers.Location = new Uri(http://www.examplepage.com);
}
base.OnActionExecuting(filterContext);
}
catch (Exception ex)
{
throw;
}
}
}
}
but nothing happens, the method that i used the attribute didn't excecute, but the page completed the post as status 200! 但没有任何反应,我使用该属性的方法没有执行,但是页面以状态200结束了该帖子! Could someone help me?
有人可以帮我吗?
Looks you are using OnActionExecuting, it is called before action method. 看起来您正在使用OnActionExecuting,它在操作方法之前被调用。 Use OnResultExecuting or OnResultExecuted.
使用OnResultExecuting或OnResultExecuted。
https://msdn.microsoft.com/en-us/library/system.web.mvc.actionfilterattribute.onresultexecuting(v=vs.118).aspx https://msdn.microsoft.com/en-us/library/system.web.mvc.actionfilterattribute.onresultexecuting(v=vs.118).aspx
Sorry, i'm did mistake. 抱歉,我做错了。 I think you can do:
我认为您可以:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Result = new RedirectResult(url);
return;
}
i think this fix in your code will be help your. 我认为您代码中的此修复将对您有所帮助。 public class SessionExpireWebApiFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute { public string UrlPortal { get { return System.Configuration.ConfigurationManager.AppSettings["Portal"].ToString();
公共类SessionExpireWebApiFilterAttribute:System.Web.Http.Filters.ActionFilterAttribute {公共字符串UrlPortal {获得{返回System.Configuration.ConfigurationManager.AppSettings [“ Portal”]。ToString(); } }
}}
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext filterContext)
{
try
{
if (StateManager.Instance.Get(Key.Autenticacao, State.Session) == null)
{
filterContext.Response = new HttpResponseMessage(HttpStatusCode.RedirectMethod);
filterContext.Response.Headers.Location = new Uri(http://www.examplepage.com);
/*add this return*/return;
}
base.OnActionExecuting(filterContext);
}
catch (Exception ex)
{
throw;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.