简体   繁体   English

总是输出特定的HTML吗?

[英]Always outputting specific HTML?

I'm facing an issue with ASP .NET MVC. 我面临ASP .NET MVC的问题。 I'm working on a technique to allow a session from recovering after it has died (expired). 我正在研究一种允许会话在死亡(过期)后恢复的技术。 For this technique to work, I need a way of being able to write to the response after a controller's action method has been invoked. 为了使这项技术起作用,我需要一种能够在调用控制器的操作方法之后写入响应的方法。

Which approach would be the best? 哪种方法最好? Is there an action-filter for this? 是否有一个动作过滤器?

If you want to use an attribute based approach, derive from ActionFilterAttribute and override either OnActionExecuted (run after the action has been executed, but before the result has been executed) or OnResultExecuted (run after the result has been executed, eg after the view has been rendered). 如果要使用基于属性的方法,请从ActionFilterAttribute派生并重写OnActionExecuted(在执行操作之后执行,但在执行结果之前运行)或OnResultExecuted(在执行结果之后执行,例如在视图执行之后)呈现)。

If you're not going the attribute way, implement IActionFilter or IResultFilter and implement the methods mentioned above. 如果您不打算使用属性方式,请实现IActionFilter或IResultFilter并实现上述方法。

You can access the output writer/output stream via filterContext.HttpContext.Response.Output or filterContext.HttpContext.Response.OutputStream respectively. 您可以分别通过filterContext.HttpContext.Response.OutputfilterContext.HttpContext.Response.OutputStream访问输出filterContext.HttpContext.Response.Output /输出流。

Create a filter that will occur after an action is excuted, like this: 创建一个将在执行操作后发生的过滤器,如下所示:

using System;

namespace Sample
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class SampleFilter : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {

            base.OnActionExecuted(filterContext);
        }
    }
}

Then decorate your action(s) like this: 然后像这样装饰您的动作:

[SampleFilter]
public virtual ActionResult Index()
{
    return View();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM