HomeController.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Diagnostics;
  2. using System.Drawing;
  3. using System.Text;
  4. using Microsoft.AspNetCore.Mvc;
  5. using QRCoder;
  6. using WebApplicationSample.Models;
  7. namespace WebApplicationSample.Controllers
  8. {
  9. public class HomeController : Controller
  10. {
  11. /// <summary>
  12. /// 前言
  13. /// </summary>
  14. public IActionResult Index()
  15. {
  16. return View();
  17. }
  18. /// <summary>
  19. /// 打赏
  20. /// </summary>
  21. public IActionResult Gratuity()
  22. {
  23. return View();
  24. }
  25. /// <summary>
  26. /// 生成二维码SVG
  27. /// </summary>
  28. /// <param name="data"></param>
  29. /// <param name="size"></param>
  30. [HttpGet]
  31. public IActionResult QrCode(string data, int size = 168)
  32. {
  33. using (var qrGenerator = new QRCodeGenerator())
  34. using (var qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.L))
  35. using (var svgQrCode = new SvgQRCode(qrCodeData))
  36. {
  37. var svgText = svgQrCode.GetGraphic(new Size(size, size));
  38. return File(Encoding.UTF8.GetBytes(svgText), "text/xml");
  39. }
  40. }
  41. public IActionResult Error()
  42. {
  43. return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  44. }
  45. }
  46. }