Skip to content

Commit 7a70209

Browse files
committed
修复返回ViewName无法找到问题
1 parent 1cd9eda commit 7a70209

File tree

4 files changed

+85
-48
lines changed

4 files changed

+85
-48
lines changed

src/Util.Webs/Filters/HtmlAttribute.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ public class HtmlAttribute : ActionFilterAttribute {
2828
/// </summary>
2929
public string Template { get; set; }
3030

31+
/// <summary>
32+
/// 视图名称,范例:/Home/Index
33+
/// </summary>
34+
public string ViewName { get; set; }
35+
36+
/// <summary>
37+
/// 是否部分视图,默认:true
38+
/// </summary>
39+
public bool IsPartialView { get; set; } = false;
40+
3141
/// <summary>
3242
/// 执行生成
3343
/// </summary>

src/Util.Webs/Razors/DefaultRazorHtmlGenerator.cs

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.AspNetCore.Mvc.ModelBinding;
1010
using Microsoft.AspNetCore.Mvc.Razor;
1111
using Microsoft.AspNetCore.Mvc.Rendering;
12+
using Microsoft.AspNetCore.Mvc.ViewEngines;
1213
using Microsoft.AspNetCore.Mvc.ViewFeatures;
1314
using Microsoft.AspNetCore.Routing;
1415
using Util.Helpers;
@@ -60,32 +61,13 @@ public async Task<string> RenderToStringAsync(RouteInformation info)
6061
var razorViewEngine = Ioc.Create<IRazorViewEngine>();
6162
var tempDataProvider = Ioc.Create<ITempDataProvider>();
6263
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-
8064
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);
8467
if (!viewResult.Success)
8568
{
8669
throw new InvalidOperationException($"找不到视图模板 {info.ActionName}");
8770
}
88-
8971
using (var stringWriter = new StringWriter())
9072
{
9173
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
@@ -107,7 +89,6 @@ public async Task WriteViewToFileAsync(RouteInformation info)
10789
var html = await RenderToStringAsync(info);
10890
if (string.IsNullOrWhiteSpace(html))
10991
return;
110-
11192
var path = Util.Helpers.Common.GetPhysicalPath(string.IsNullOrWhiteSpace(info.FilePath) ? GetPath(info) : info.FilePath);
11293
var directory = System.IO.Path.GetDirectoryName(path);
11394
if (string.IsNullOrWhiteSpace(directory))
@@ -122,6 +103,11 @@ public async Task WriteViewToFileAsync(RouteInformation info)
122103
}
123104
}
124105

106+
/// <summary>
107+
/// 获取Html默认生成路径
108+
/// </summary>
109+
/// <param name="info">路由信息</param>
110+
/// <returns></returns>
125111
protected virtual string GetPath(RouteInformation info)
126112
{
127113
var area = info.AreaName.SafeString();
@@ -130,5 +116,41 @@ protected virtual string GetPath(RouteInformation info)
130116
var path = info.TemplatePath.Replace("{area}", area).Replace("{controller}", controller).Replace("{action}", action);
131117
return path.ToLower();
132118
}
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+
}
133155
}
134156
}

src/Util.Webs/Razors/RouteAnalyzer.cs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,62 +39,57 @@ public IEnumerable<RouteInformation> GetAllRouteInformations()
3939
var actionDescriptors = this._actionDescriptorCollectionProvider.ActionDescriptors.Items;
4040
foreach (var actionDescriptor in actionDescriptors)
4141
{
42-
RouteInformation info=new RouteInformation();
43-
42+
RouteInformation info = new RouteInformation();
4443
if (actionDescriptor.RouteValues.ContainsKey("area"))
4544
{
4645
info.AreaName = actionDescriptor.RouteValues["area"];
4746
}
48-
4947
// Razor页面路径以及调用
5048
if (actionDescriptor is PageActionDescriptor pageActionDescriptor)
5149
{
5250
info.Path = pageActionDescriptor.ViewEnginePath;
5351
info.Invocation = pageActionDescriptor.RelativePath;
5452
}
55-
5653
// 路由属性路径
5754
if (actionDescriptor.AttributeRouteInfo != null)
5855
{
5956
info.Path = $"/{actionDescriptor.AttributeRouteInfo.Template}";
6057
}
61-
6258
// Controller/Action 的路径以及调用
6359
if (actionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
6460
{
6561
if (info.Path.IsEmpty())
6662
{
67-
info.Path =
68-
$"/{controllerActionDescriptor.ControllerName}/{controllerActionDescriptor.ActionName}";
69-
}
70-
71-
var controllerHtmlAttribute = controllerActionDescriptor.ControllerTypeInfo.GetCustomAttribute<HtmlAttribute>();
72-
73-
if (controllerHtmlAttribute != null)
74-
{
75-
info.FilePath = controllerHtmlAttribute.Path;
76-
info.TemplatePath = controllerHtmlAttribute.Template;
77-
}
78-
79-
var htmlAttribute = controllerActionDescriptor.MethodInfo.GetCustomAttribute<HtmlAttribute>();
80-
81-
if (htmlAttribute != null)
82-
{
83-
info.FilePath = htmlAttribute.Path;
84-
info.TemplatePath = htmlAttribute.Template;
63+
info.Path = $"/{controllerActionDescriptor.ControllerName}/{controllerActionDescriptor.ActionName}";
8564
}
86-
65+
SetHtmlInfo(info, controllerActionDescriptor);
8766
info.ControllerName = controllerActionDescriptor.ControllerName;
8867
info.ActionName = controllerActionDescriptor.ActionName;
8968
info.Invocation = $"{controllerActionDescriptor.ControllerName}Controller.{controllerActionDescriptor.ActionName}";
9069
}
91-
9270
info.Invocation += $"({actionDescriptor.DisplayName})";
93-
9471
list.Add(info);
9572
}
9673

9774
return list;
9875
}
76+
77+
/// <summary>
78+
/// 设置Html信息
79+
/// </summary>
80+
/// <param name="routeInformation">路由信息</param>
81+
/// <param name="controllerActionDescriptor">控制器</param>
82+
private void SetHtmlInfo(RouteInformation routeInformation,
83+
ControllerActionDescriptor controllerActionDescriptor)
84+
{
85+
var htmlAttribute = controllerActionDescriptor.ControllerTypeInfo.GetCustomAttribute<HtmlAttribute>() ??
86+
controllerActionDescriptor.MethodInfo.GetCustomAttribute<HtmlAttribute>();
87+
if (htmlAttribute == null)
88+
return;
89+
routeInformation.FilePath = htmlAttribute.Path;
90+
routeInformation.TemplatePath = htmlAttribute.Template;
91+
routeInformation.IsPartialView = htmlAttribute.IsPartialView;
92+
routeInformation.ViewName = htmlAttribute.ViewName;
93+
}
9994
}
10095
}

src/Util.Webs/Razors/RouteInformation.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,15 @@ public class RouteInformation
4343
/// 模板路径
4444
/// </summary>
4545
public string TemplatePath { get; set; }
46+
47+
/// <summary>
48+
/// 视图名称
49+
/// </summary>
50+
public string ViewName { get; set; }
51+
52+
/// <summary>
53+
/// 是否部分视图
54+
/// </summary>
55+
public bool IsPartialView { get; set; } = false;
4656
}
4757
}

0 commit comments

Comments
 (0)