/
DefaultViewAttribute.cs
37 lines (33 loc) · 1.23 KB
/
DefaultViewAttribute.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
using System;
using ServiceStack.Web;
namespace ServiceStack;
/// <summary>
/// Change the default HTML view or template used for the HTML response of this service
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public class DefaultViewAttribute : RequestFilterAttribute
{
public string View { get; set; }
public string Template { get; set; }
public DefaultViewAttribute() : this(null,null) { }
public DefaultViewAttribute(string view) : this(view, null) { }
public DefaultViewAttribute(string view, string template)
{
View = view;
Template = template;
Priority = -1;
}
public override void Execute(IRequest req, IResponse res, object requestDto)
{
if (!string.IsNullOrEmpty(View))
{
if (!req.Items.TryGetValue(Keywords.View, out var currentView) || string.IsNullOrEmpty(currentView as string))
req.SetView(View);
}
if (!string.IsNullOrEmpty(Template))
{
if (!req.Items.TryGetValue(Keywords.Template, out var currentTemplate) || string.IsNullOrEmpty(currentTemplate as string))
req.SetTemplate(Template);
}
}
}