HomeController.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. /// 生成二维码SVG
  20. /// </summary>
  21. /// <param name="data"></param>
  22. /// <param name="size"></param>
  23. [HttpGet]
  24. public IActionResult QrCode(string data, int size = 168)
  25. {
  26. using (var qrGenerator = new QRCodeGenerator())
  27. using (var qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.L))
  28. using (var svgQrCode = new SvgQRCode(qrCodeData))
  29. {
  30. var svgText = svgQrCode.GetGraphic(new Size(size, size));
  31. return File(Encoding.UTF8.GetBytes(svgText), "text/xml");
  32. }
  33. }
  34. public IActionResult Error()
  35. {
  36. return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  37. }
  38. }
  39. }