6
6
namespace DasBlog . Web . TagHelpers . Post
7
7
{
8
8
public class PostImageTagHelper : TagHelper
9
-
10
9
{
11
10
public PostViewModel Post { get ; set ; }
12
11
12
+ public string DefaultImage { get ; set ; }
13
13
public string Css { get ; set ; }
14
+ public string Style { get ; set ; }
14
15
15
16
private readonly IDasBlogSettings dasBlogSettings ;
16
17
@@ -19,19 +20,39 @@ public PostImageTagHelper(IDasBlogSettings dasBlogSettings)
19
20
this . dasBlogSettings = dasBlogSettings ;
20
21
}
21
22
22
- public override async Task ProcessAsync ( TagHelperContext context , TagHelperOutput output )
23
+ public override void Process ( TagHelperContext context , TagHelperOutput output )
23
24
{
24
- output . TagMode = TagMode . StartTagAndEndTag ;
25
+ var imgUrl = Post . ImageUrl ;
26
+
27
+ output . TagMode = TagMode . SelfClosing ;
25
28
output . TagName = "img" ;
26
29
30
+ if ( ! string . IsNullOrEmpty ( imgUrl ) )
31
+ {
32
+ output . Attributes . SetAttribute ( "src" , dasBlogSettings . RelativeToRoot ( imgUrl ) ) ;
33
+ }
34
+ else
35
+ {
36
+ if ( ! string . IsNullOrEmpty ( DefaultImage ) )
37
+ {
38
+ output . Attributes . SetAttribute ( "src" , DefaultImage ) ;
39
+ }
40
+ }
41
+
27
42
if ( ! string . IsNullOrEmpty ( Css ) )
28
43
{
29
44
output . Attributes . SetAttribute ( "class" , Css ) ;
30
45
}
31
46
32
- output . Attributes . SetAttribute ( "src" , dasBlogSettings . RelativeToRoot ( Post . ImageUrl ) ) ;
33
- output . Attributes . SetAttribute ( "alt" , Post . Title ) ;
34
- await Task . CompletedTask ;
47
+ if ( ! string . IsNullOrEmpty ( Style ) )
48
+ {
49
+ output . Attributes . SetAttribute ( "style" , Style ) ;
50
+ }
51
+ }
52
+
53
+ public override Task ProcessAsync ( TagHelperContext context , TagHelperOutput output )
54
+ {
55
+ return Task . Run ( ( ) => Process ( context , output ) ) ;
35
56
}
36
57
}
37
58
}
0 commit comments