Skip to content

Instantly share code, notes, and snippets.

Avatar
🏡
Working remote and staying safe

Jason N. Gaylord jasongaylord

🏡
Working remote and staying safe
View GitHub Profile
View TwitterOptions.cs
public class TwitterOptions
{
[Required]
public string ConsumerKey { get; set; } = "";
[Required]
public string ConsumerSecret { get; set; } = "";
public string Username { get; set; }
}
View TwitterViewComponent.csproj
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.1.3" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" />
View RetrieveTweetsViewComponent.cs
public class RetrieveTweetsViewComponent : ViewComponent
{
protected TwitterOptions TwitterOptions { get; private set; }
public RetrieveTweetsViewComponent(IOptions<TwitterOptions> options = null)
{
if (options != null)
TwitterOptions = options.Value;
else
TwitterOptions = new TwitterOptions();
View ServiceCollectionExtensions.cs
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using TwitterViewComponent;
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddTwitter(this IServiceCollection services, IConfiguration configuration = null)
{
View Index.cshtml
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
@await Component.InvokeAsync("RetrieveTweets", new { })
View appsettings.json
{
"TwitterOptions": {
"Username": "jgaylord",
"ConsumerKey": "YOUR_KEY",
"ConsumerSecret": "YOUR_SECRET"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
View Default.cshtml
@model List<TwitterViewComponent.Tweet>
<h3>Recent Tweets</h3>
<ul>
@foreach (var tweet in Model)
{
<li>
@tweet.Message<br/>
<a href="http://twitter.com/@tweet.Username/statuses/@tweet.Id">@(tweet.Timestamp)</a>
</li>
@jasongaylord
jasongaylord / Default.cshtml
Created Nov 5, 2018
Twitter View Component
View Default.cshtml
@model List<TwitterViewComponent.Tweet>
<h3>Recent Tweets</h3>
<ul>
@foreach (var tweet in Model)
{
<li>
@tweet.Message<br/>
<a href="http://twitter.com/@tweet.Username/statuses/@tweet.Id">@(tweet.Timestamp)</a>
</li>
@jasongaylord
jasongaylord / DateTimeExtensions.cs
Created Nov 5, 2018
A DateTime extension to show a friendly date based on the value passed in and DateTime.Now
View DateTimeExtensions.cs
public static class DateTimeExtensions
{
public static string ToFriendlyDate(this DateTime date)
{
// Calculate the elapsed time
TimeSpan s = DateTime.UtcNow.Subtract(date);
// Get total number of days elapsed.
int dayDiff = (int)s.TotalDays;