简体   繁体   English

使用Asp.net Webforms中的属性和web.api重定向到网页

[英]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.

相关问题 使用Asp.net MVC web.api为什么要重定向而不是只调用其他函数? - Using Asp.net MVC web.api why should I redirect rather than just call other functions? 即使具有自定义验证属性,Asp.net Web.APi也会返回内置错误响应 - Asp.net Web.APi returns build in error response even with custom validation attribute ASP.NET Core Web.API中的拦截调用者语言 - Intercept caller language in ASP.NET Core Web.API 带有WEB.API和Castle Windsor容器的ASP.NET MVC - ASP.NET MVC with WEB.API and Castle Windsor container 使用 asp.net web.api json 不起作用 - Consuming asp.net web.api json not working WEB API + ASP.NET尝试以json格式显示来自WEB.API的数据 - WEB API + ASP.NET trying to display data from WEB.API in json format 我可以在Asp.net Web.API中的AppStart一次自定义RequestTelemetry属性吗? - Can I customize RequestTelemetry properties once at AppStart in Asp.net Web.API ASP.NET Core 2.1 Web.API更改应用程序见解的日志记录检测键 - ASP.NET Core 2.1 Web.API change application insights instrumentation key for logging ASP.NET web.api帮助页面不显示任何描述 - ASP.NET web.api Help Page does not show any description 如何在AuthorAttribute的ASP.NET Web.API MVC 5的IsAuthorized上获取Post参数 - How to get Post parameters on IsAuthorized of AuthorizeAttribute ASP.NET Web.API MVC 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM