-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathIndex.cshtml.cs
More file actions
66 lines (54 loc) · 2.25 KB
/
Index.cshtml.cs
File metadata and controls
66 lines (54 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Diagnostics.Metrics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using Microsoft.FeatureManagement;
namespace VariantAndAzureMonitorDemo.Pages
{
public class IndexModel : PageModel
{
private readonly IVariantFeatureManager _featureManager;
private readonly Meter _meter;
private readonly ILogger<IndexModel> _logger;
public IndexModel(IVariantFeatureManager featureManager, IMeterFactory meterFactory, ILogger<IndexModel> logger)
{
_featureManager = featureManager ?? throw new ArgumentNullException(nameof(featureManager));
_meter = meterFactory?.Create("VariantAndAzureMonitorDemo") ?? throw new ArgumentNullException(nameof(meterFactory));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public string Username { get; set; }
public async Task<IActionResult> OnGet()
{
Username = HttpContext.User.Identity.Name;
if (string.IsNullOrEmpty(Username))
{
return Redirect("/RandomizeUser");
}
//
// Use application's feature manager to get assigned variant for current user
Variant variant = await _featureManager
.GetVariantAsync("ImageRating", HttpContext.RequestAborted);
//
// Set the page's display image based on the assigned variant.
ViewData["ImageUri"] = variant.Configuration.Value;
return Page();
}
public IActionResult OnPost()
{
if (Request.Form != null)
{
string val = Request.Form["imageScore"];
if (val != null &&
int.TryParse(val, out int rating))
{
// Create a histogram to track the image rating
var imageRatingHistogram = _meter.CreateHistogram<long>("ImageRating");
imageRatingHistogram.Record(rating);
// Track the vote event using ILogger custom event
_logger.LogVote(rating);
}
}
return Redirect("/RandomizeUser");
}
}
}