![](/img/trans.png)
[英]Using Asp.net MVC web.api why should I redirect rather than just call other functions?
[英]Redirect to a webpage using an attribute in Asp.net webforms with web.api
我創建了此屬性類以重定向到網頁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;
}
}
}
}
但沒有任何反應,我使用該屬性的方法沒有執行,但是頁面以狀態200結束了該帖子! 有人可以幫我嗎?
看起來您正在使用OnActionExecuting,它在操作方法之前被調用。 使用OnResultExecuting或OnResultExecuted。
抱歉,我做錯了。 我認為您可以:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Result = new RedirectResult(url);
return;
}
我認為您代碼中的此修復將對您有所幫助。 公共類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.