问题场景见下面的示例代码:public class ErrorsController : Controller{ public IActionResult StatusCodePage(int? statusCode) { bool viewExists = false; //TODO:检查对应于statusCode的视图文件是否存在
return viewExists ? View(statusCode.Value) : View("Default");
}
}
请问如何实现?
1 回答
缥缈止盈
TA贡献2041条经验 获得超4个赞
通过 ICompositeViewEngine.FindView() 解决了
public class ErrorsController : Controller{ public IActionResult StatusCodePage(int? statusCode, [FromServices]ICompositeViewEngine compositeViewEngine) { bool viewExists = statusCode.HasValue && compositeViewEngine.FindView(ControllerContext, statusCode.Value.ToString(), true).Success; return viewExists ? View(statusCode.Value.ToString()) : View("Default"); } }
- 1 回答
- 0 关注
- 719 浏览
添加回答
举报
0/150
提交
取消