9
9
using Microsoft . AspNetCore . Mvc . ModelBinding ;
10
10
using Microsoft . AspNetCore . Mvc . Razor ;
11
11
using Microsoft . AspNetCore . Mvc . Rendering ;
12
+ using Microsoft . AspNetCore . Mvc . ViewEngines ;
12
13
using Microsoft . AspNetCore . Mvc . ViewFeatures ;
13
14
using Microsoft . AspNetCore . Routing ;
14
15
using Util . Helpers ;
@@ -60,32 +61,13 @@ public async Task<string> RenderToStringAsync(RouteInformation info)
60
61
var razorViewEngine = Ioc . Create < IRazorViewEngine > ( ) ;
61
62
var tempDataProvider = Ioc . Create < ITempDataProvider > ( ) ;
62
63
var serviceProvider = Ioc . Create < IServiceProvider > ( ) ;
63
-
64
- var routeData = new RouteData ( ) ;
65
- if ( ! info . AreaName . IsEmpty ( ) )
66
- {
67
- routeData . Values . Add ( "area" , info . AreaName ) ;
68
- }
69
-
70
- if ( ! info . ControllerName . IsEmpty ( ) )
71
- {
72
- routeData . Values . Add ( "controller" , info . ControllerName ) ;
73
- }
74
-
75
- if ( ! info . ActionName . IsEmpty ( ) )
76
- {
77
- routeData . Values . Add ( "action" , info . ActionName ) ;
78
- }
79
-
80
64
var httpContext = new DefaultHttpContext { RequestServices = serviceProvider } ;
81
- var actionContext = new ActionContext ( httpContext , routeData , new ActionDescriptor ( ) ) ;
82
-
83
- var viewResult = razorViewEngine . FindView ( actionContext , info . ActionName , true ) ;
65
+ var actionContext = new ActionContext ( httpContext , GetRouteData ( info ) , new ActionDescriptor ( ) ) ;
66
+ var viewResult = GetView ( razorViewEngine , actionContext , info ) ;
84
67
if ( ! viewResult . Success )
85
68
{
86
69
throw new InvalidOperationException ( $ "找不到视图模板 { info . ActionName } ") ;
87
70
}
88
-
89
71
using ( var stringWriter = new StringWriter ( ) )
90
72
{
91
73
var viewDictionary = new ViewDataDictionary ( new EmptyModelMetadataProvider ( ) , new ModelStateDictionary ( ) ) ;
@@ -107,7 +89,6 @@ public async Task WriteViewToFileAsync(RouteInformation info)
107
89
var html = await RenderToStringAsync ( info ) ;
108
90
if ( string . IsNullOrWhiteSpace ( html ) )
109
91
return ;
110
-
111
92
var path = Util . Helpers . Common . GetPhysicalPath ( string . IsNullOrWhiteSpace ( info . FilePath ) ? GetPath ( info ) : info . FilePath ) ;
112
93
var directory = System . IO . Path . GetDirectoryName ( path ) ;
113
94
if ( string . IsNullOrWhiteSpace ( directory ) )
@@ -122,6 +103,11 @@ public async Task WriteViewToFileAsync(RouteInformation info)
122
103
}
123
104
}
124
105
106
+ /// <summary>
107
+ /// 获取Html默认生成路径
108
+ /// </summary>
109
+ /// <param name="info">路由信息</param>
110
+ /// <returns></returns>
125
111
protected virtual string GetPath ( RouteInformation info )
126
112
{
127
113
var area = info . AreaName . SafeString ( ) ;
@@ -130,5 +116,41 @@ protected virtual string GetPath(RouteInformation info)
130
116
var path = info . TemplatePath . Replace ( "{area}" , area ) . Replace ( "{controller}" , controller ) . Replace ( "{action}" , action ) ;
131
117
return path . ToLower ( ) ;
132
118
}
119
+
120
+ /// <summary>
121
+ /// 获取Razor视图
122
+ /// </summary>
123
+ /// <param name="razorViewEngine">Razor视图引擎</param>
124
+ /// <param name="actionContext">操作上下文</param>
125
+ /// <param name="info">路由信息</param>
126
+ /// <returns></returns>
127
+ protected virtual ViewEngineResult GetView ( IRazorViewEngine razorViewEngine , ActionContext actionContext , RouteInformation info )
128
+ {
129
+ return razorViewEngine . FindView ( actionContext , info . ViewName . IsEmpty ( ) ? info . ActionName : info . ViewName ,
130
+ ! info . IsPartialView ) ;
131
+ }
132
+
133
+ /// <summary>
134
+ /// 获取路由数据
135
+ /// </summary>
136
+ /// <param name="info">路由信息</param>
137
+ /// <returns></returns>
138
+ protected virtual RouteData GetRouteData ( RouteInformation info )
139
+ {
140
+ var routeData = new RouteData ( ) ;
141
+ if ( ! info . AreaName . IsEmpty ( ) )
142
+ {
143
+ routeData . Values . Add ( "area" , info . AreaName ) ;
144
+ }
145
+ if ( ! info . ControllerName . IsEmpty ( ) )
146
+ {
147
+ routeData . Values . Add ( "controller" , info . ControllerName ) ;
148
+ }
149
+ if ( ! info . ActionName . IsEmpty ( ) )
150
+ {
151
+ routeData . Values . Add ( "action" , info . ActionName ) ;
152
+ }
153
+ return routeData ;
154
+ }
133
155
}
134
156
}
0 commit comments