forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRazorHandler.cs
More file actions
62 lines (52 loc) · 1.94 KB
/
RazorHandler.cs
File metadata and controls
62 lines (52 loc) · 1.94 KB
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
using System;
using System.Net;
using System.Threading.Tasks;
using ServiceStack.Host.Handlers;
using ServiceStack.Razor.Managers;
using ServiceStack.Web;
namespace ServiceStack.Razor
{
public class RazorHandler : ServiceStackHandlerBase
{
public Action<IRequest> Filter { get; set; }
public RazorFormat RazorFormat { get; set; }
public RazorPage RazorPage { get; set; }
public object Model { get; set; }
public string PathInfo { get; set; }
public RazorHandler(string pathInfo)
{
PathInfo = pathInfo;
}
public override bool RunAsAsync() => true;
public override async Task ProcessRequestAsync(IRequest httpReq, IResponse httpRes, string operationName)
{
if (HostContext.ApplyCustomHandlerRequestFilters(httpReq, httpRes))
return;
Filter?.Invoke(httpReq);
httpRes.ContentType = MimeTypes.Html;
if (RazorFormat == null)
RazorFormat = RazorFormat.Instance;
var contentPage = RazorPage ?? RazorFormat.GetContentPage(PathInfo);
if (contentPage == null)
{
httpRes.StatusCode = (int)HttpStatusCode.NotFound;
httpRes.EndHttpHandlerRequest();
return;
}
var model = Model;
if (model == null)
httpReq.Items.TryGetValue("Model", out model);
if (model == null)
{
var modelType = RazorPage?.ModelType;
model = modelType == null || modelType == typeof(DynamicRequestObject)
? null
: await DeserializeHttpRequestAsync(modelType, httpReq, httpReq.ContentType);
}
using (RazorFormat.ProcessRazorPage(httpReq, contentPage, model, httpRes))
{
httpRes.EndHttpHandlerRequest(skipHeaders: true);
}
}
}
}