Skip to content

Commit 39ca97f

Browse files
Added Feedback to ASP.NET Core MVC sample
1 parent 498dd04 commit 39ca97f

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

samples/Sentry.Samples.AspNetCore.Mvc/Controllers/HomeController.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Data;
22
using System.Diagnostics;
33
using Microsoft.AspNetCore.Mvc;
4+
using Samples.AspNetCore.Mvc.Models;
45
using Sentry.Ben.BlockingDetector;
56
using Sentry.Samples.AspNetCore.Mvc.Models;
67

@@ -176,4 +177,33 @@ public IActionResult Error()
176177
{
177178
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
178179
}
180+
181+
[HttpGet("[controller]/feedback")]
182+
public IActionResult Feedback()
183+
{
184+
return View();
185+
}
186+
187+
[HttpPost]
188+
public async Task<IActionResult> SubmitFeedback(FeedbackModel feedback)
189+
{
190+
if (!ModelState.IsValid)
191+
{
192+
return View("Feedback", feedback);
193+
}
194+
195+
var sentryFeedback = new SentryFeedback(feedback.Message!, feedback.ContactEmail, feedback.Name);
196+
var hint = new SentryHint();
197+
198+
if (feedback.Screenshot is { Length: > 0 })
199+
{
200+
await using var memoryStream = new MemoryStream();
201+
await feedback.Screenshot.CopyToAsync(memoryStream);
202+
hint.AddAttachment(memoryStream.ToArray(), feedback.Screenshot.FileName, AttachmentType.Default, "image/png");
203+
}
204+
205+
SentrySdk.CaptureFeedback(sentryFeedback, null, hint);
206+
ViewBag.Message = "Feedback submitted successfully!";
207+
return View("Index");
208+
}
179209
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace Samples.AspNetCore.Mvc.Models;
4+
5+
public class FeedbackModel
6+
{
7+
[Required]
8+
public string? Message { get; set; }
9+
10+
[EmailAddress]
11+
public string? ContactEmail { get; set; }
12+
13+
public string? Name { get; set; }
14+
15+
public IFormFile? Screenshot { get; set; }
16+
}

samples/Sentry.Samples.AspNetCore.Mvc/Sentry.Samples.AspNetCore.Mvc.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@
1818
<ProjectReference Include="..\..\src\Sentry.AspNetCore\Sentry.AspNetCore.csproj" />
1919
</ItemGroup>
2020

21+
<ItemGroup>
22+
<AdditionalFiles Include="Views\Home\Feedback.cshtml" />
23+
</ItemGroup>
24+
2125
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@model Samples.AspNetCore.Mvc.Models.FeedbackModel
2+
3+
@{
4+
ViewData["Title"] = "Submit Feedback";
5+
}
6+
7+
<h2>Submit Feedback</h2>
8+
9+
@if (ViewBag.Message != null)
10+
{
11+
<div class="alert alert-success">@ViewBag.Message</div>
12+
}
13+
14+
<form asp-action="SubmitFeedback" method="post" enctype="multipart/form-data">
15+
<div class="form-group">
16+
<label asp-for="Message"></label>
17+
<textarea asp-for="Message" class="form-control"></textarea>
18+
<span asp-validation-for="Message" class="text-danger"></span>
19+
</div>
20+
<div class="form-group">
21+
<label asp-for="ContactEmail"></label>
22+
<input asp-for="ContactEmail" class="form-control" />
23+
<span asp-validation-for="ContactEmail" class="text-danger"></span>
24+
</div>
25+
<div class="form-group">
26+
<label asp-for="Name"></label>
27+
<input asp-for="Name" class="form-control" />
28+
<span asp-validation-for="Name" class="text-danger"></span>
29+
</div>
30+
<div class="form-group">
31+
<label asp-for="Screenshot">Attach Screenshot</label>
32+
<input asp-for="Screenshot" type="file" class="form-control" />
33+
<span asp-validation-for="Screenshot" class="text-danger"></span>
34+
</div>
35+
<button type="submit" class="btn btn-primary">Submit</button>
36+
</form>

samples/Sentry.Samples.AspNetCore.Mvc/Views/Shared/_Layout.cshtml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<li class="nav-item">
3232
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Action">Action Throws</a>
3333
</li>
34+
<li class="nav-item">
35+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Feedback">Submit Feedback</a>
36+
</li>
3437
</ul>
3538
</div>
3639
</div>

0 commit comments

Comments
 (0)