問題場(chǎng)景見下面的示例代碼:public class ErrorsController : Controller{ public IActionResult StatusCodePage(int? statusCode) { bool viewExists = false; //TODO:檢查對(duì)應(yīng)于statusCode的視圖文件是否存在
return viewExists ? View(statusCode.Value) : View("Default");
}
}
請(qǐng)問如何實(shí)現(xiàn)?
1 回答

縹緲止盈
TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊
通過 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 關(guān)注
- 757 瀏覽
添加回答
舉報(bào)
0/150
提交
取消