1

Maybe it´s a strange question, but imagine this:

//We all know that View is a method...
public ActionResult Something()
{
    return View("index");
}

But what if I step before this method to perform some stats

public ActionResult Something()
{
    return PerformStats(View("index"));
}

I will have a private method like this:

private ActionResult PerformStats(ViewResult viewResult)
{

    //THIS IS WHAT I WANT TO ACCHIEVE:
    //*********************************

    var contentSent = viewResult.InnerHtml.Lengh; <<-- I wish!

    return viewResult;
}

And latter, what i want to do, is to save that ammount of content sent to the client. It doesn´t matter if it is the exactly quantity of html, even if I get the .count() of a json it will do the trick.

Is any way to know the rendered content on the controller?

Thanks!

4

1 回答 1

1

OnActionExecuting:在动作方法执行之前调用。您可以在其中放置与统计信息相关的逻辑。

http://msdn.microsoft.com/en-us/library/system.web.mvc.iactionfilter.onactionexecuting(v=vs.98).aspx

OnActionExecuted: 在动作方法执行后调用。

http://msdn.microsoft.com/en-us/library/system.web.mvc.iactionfilter.onactionexecuted(v=vs.98).aspx

在这些方法中,您可以访问ActionExecutingActionExecutedContext

如果您想获得一定大小的渲染 HTML(部分或完整视图),那么您可能需要:

  • 找到要渲染的视图
  • 将其存储在字符串生成器中
  • 获取它的长度

有一个问题解释了如何在动作方法中将视图呈现为字符串:在 MVC3 Razor 中,如何在动作中获取呈现视图的 html?

于 2013-11-12T17:00:53.657 回答