Diskuze: ASP .NET Core 3.1 - Detekcia kliknutia na link
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
Zobrazeno 6 zpráv z 6.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
Tu je kód, ak to pomôže:
@using ManagerRSS.Models.Classes
@model ItemRSS[]
<div class="table-responsive">
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Title</th>
<th scope="col">Link</th>
<th scope="col">Description</th>
<th scope="col">Public date</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Length; i++)
{
<tr>
<th scope="row">@i</th>
<td>@Model[i].Title</td>
<td><a href="@Model[i].Link" target="_blank">Go web</a></td>
<td>@Model[i].Description</td>
<td>@Model[i].PubDate</td>
</tr>
}
</tbody>
</table>
</div>
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace ManagerRSS.Models.Classes
{
public class DocRSS
{
/// <summary>
/// Items of document.
/// </summary>
public ItemRSS[] Items { get; private set; }
/// <summary>
///
/// </summary>
/// <param name="url">URL of the xml document.</param>
public DocRSS(string url)
{
Items = ExtractItems(url);
}
/// <summary>
/// Extracts elements from an xml document.
/// </summary>
/// <param name="url">URL of the xml document.</param>
/// <returns></returns>
private ItemRSS[] ExtractItems(string url)
{
List<ItemRSS> items = new List<ItemRSS>();
XDocument xDocument = XDocument.Load(url);
foreach (XElement element in xDocument.Element("rss").Element("channel").Elements("item"))
{
ItemRSS item = new ItemRSS()
{
Title = element.Element("title").Value,
Link = element.Element("link").Value,
Description = element.Element("description").Value,
PubDate = DateTime.Parse(element.Element("pubDate").Value)
};
items.Add(item);
}
return items.ToArray();
}
}
}
using ManagerRSS.Models.Classes;
using Microsoft.AspNetCore.Mvc;
namespace ManagerRSS.Controllers
{
/// <summary>
/// Controller for read RSS xml document.
/// </summary>
public class ReaderRSSController : Controller
{
public IActionResult Index()
{
DocRSS doc = new DocRSS("https://example.com/rss");
return View(doc.Items);
}
}
}
Vyzerá to naozaj zaujímavo. Nevedel sem že niečo také existuje. Zbehnem dokumentáciu a poprípade to označím ako správnu odpoveď.
Až zjistíš, že použití Blazor není ta správná cesta (neb je to kanón na vrabce) a že vůbec nepotřebuješ detekovat kliknutí na link, předělej to na:
<td><a href="/clickHandler?url=@Model[i].Link" target="_blank">Go web</a></td>
Přičemž clickHandler povede na action, jejímž výsledkem bude redirect na ten "url". Takto to dělá Google.
Zobrazeno 6 zpráv z 6.