-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
Index.cshtml.cs
67 lines (61 loc) · 2.02 KB
/
Index.cshtml.cs
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
67
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.AspNetCore.Mvc.RazorPages;
using SimpleFeedReader.Services;
using SimpleFeedReader.ViewModels;
namespace SimpleFeedReader.Pages
{
// <SnippetIndexModelStart>
public class IndexModel : PageModel
{
private readonly NewsService _newsService;
public IndexModel(NewsService newsService)
{
_newsService = newsService;
}
public string ErrorText { get; private set; }
public List<NewsStoryViewModel> NewsItems { get; private set; }
public async Task OnGet()
{
string feedUrl = Request.Query["feedurl"];
if (!string.IsNullOrEmpty(feedUrl))
{
try
{
NewsItems = await _newsService.GetNews(feedUrl);
}
catch (UriFormatException)
{
ErrorText = "There was a problem parsing the URL.";
return;
}
catch (WebException ex) when (ex.Status == WebExceptionStatus.NameResolutionFailure)
{
ErrorText = "Unknown host name.";
return;
}
catch (WebException ex) when (ex.Status == WebExceptionStatus.ProtocolError)
{
ErrorText = "Syndication feed not found.";
return;
}
catch (AggregateException ae)
{
ae.Handle((x) =>
{
if (x is XmlException)
{
ErrorText = "There was a problem parsing the feed. Are you sure that URL is a syndication feed?";
return true;
}
return false;
});
}
}
}
}
// </SnippetIndexModelStart>
}