Yes, CourtesyFlush is available is NuGet package which can
able to flash out content from Action. So, how does it can improve performances?
Well, think about one application where some heavy weight
processing is being performed within some controller. As we know the nature of
MVC, Once operation within action get finish, it returns view and the view get
render in UI.
Now, the problem is, the UI will not get render until the
heavy operation completes. It would good
if we could show something in time of execution of heavy work.
Here is the solution, give reference of CourtesyFlush
package in your application. Here is command to install it.
Ø
Install-Package CourtesyFlush
After installation just give reference of below namespace in application.
using CourtesyFlush;
Let’s have a look on class definition for CourtesyFlush.
There are 6 functions (basically 2 functions) where FlushHead () is overloaded
5 times in 5 different flavours.
Let us have a look on, how it works. As the class and the
functions are static, we will call the function using this operator. Here we
will use flush function to flash one view in time of execution of process. Here is sample code.
public ActionResult Index()
{
ViewResult vr = new ViewResult();
vr.ViewName = "_data";
this.Flush(vr);
System.Threading.Thread.Sleep(5000);
ViewBag.message = "long process completed";
return View();
}
Just have a look that before the Sleep(500) function(heavy
task) we are flushing some output in UI and the view (data view) is getting
render in UI immediately.
So, this is the beauty of Flushing concept. Oh, the _data
view is plain .cshtml view, which is located within view folder under proper
controller name.
Now, let’s see how we can use FlushHead() extension method.
Here is one working example.
public ActionResult Index()
{
//_Head
view is located within
//Shared
folder under view
this.FlushHead();
System.Threading.Thread.Sleep(5000);
ViewBag.message = "long process completed";
return View();
}
As, seen in class definition , there are five flavor of
FlushHead() function and we are implementing the first one, which does not talk
any argument.
If we don not pass any argument then it will search for _Head
view within Shared folder. I have kept
one same in my shared view folder already.
Again, the concept of flush is very similar, we are seeing
some content in UI even before execution of Action.
In this example, we will not discuss all overloaded function
of FlushHead() but those are very easy and self explanatory to implement.
No comments:
Post a Comment