Adding a estimated time to read to a .NET Core Blog with Razor


Post Image

This week i implemented a feature what shows you, how many time you have to spend, to read a blog article.

In the first step, i used my model BlogStory to get the content. Then Razor does the following:

It counts the spaces between the words and adds 1. So now we knowing how many words we have in that article. The most people can read 200 to 250 in one minute. So we need to divide the counted words with 250. Then we knowing, how many minutes it takes to read. Then we combine a modulo with a divide to get the seconds.

 @{ var word_count = @Model.Body;
var counts = word_count.Count(ch => ch == ' ') + 1;
var minutes = counts / 250; var seconds = counts % 250 / (250 / 60);
var str_minutes = (minutes == 1) ? "Minute" : "Minutes";
var str_seconds = (seconds == 1) ? "Second" : "Seconds";
}

Now we placing the code for displaying the estimated time to read

  @minutes @str_minutes @seconds @str_seconds 

On the place, where this snipped is, will be the estimated time to read.

Currently it is placed on client side. It is planned to move that function to server side in future.


If you liked this article, so Buy me a coffee :-).

Sascha Wappen

Sascha Zyroslaw Kyrill Manns



Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.



Feeds


Syndication

  • add2netvibes
  • add2yahoo
  • Follow me on Blogarama
  • Blogverzeichnis - Bloggerei.de
  • Blogging Fusion Blog Directory
  • Blogverzeichnis
  • TopBlogs.de das Original - Blogverzeichnis | Blog Top Liste
  • Sascha Mannss Twilight Zone - OnToplist.com

Badges

Visitors:

counter

Stackoverflow

profile for saigkill at Stack Overflow, Q&A for professional and enthusiast programmers


Codeproject


OpenHUB

Open Hub profile for Sascha Manns


Application Name MannsBlog Environment Name Productive
Application Ver 5.0.4.0 Runtime Framework x86
App Path C:\home\site\wwwroot\ Runtime Version .NET 5.0.9
Operating System Microsoft Windows 10.0.14393 Runtime Arch X86