【发布时间】:2020-06-12 14:43:35
【问题描述】:
我想在视图组件中设置状态码,以便我可以在 .ts 文件中检查。
这是我的 .ts 文件代码
window.onload = function () {
fetch('../Controller/ActionName',
{
headers: {
RequestVerificationToken: (<HTMLInputElement>document.getElementById("Token")).value
}
})
.then((response) => {
if (response.status != 200) {
redirectToHomePage();
}
response.text().then((data) => {
document.getElementById("Id")!.innerHTML = data;
});
});
// Redirect to home page
function redirectToHomePage() {
var url = window.location.origin;
window.location.href = url + '/Login/Login/';
}
};
Action 将调用 ViewComponent 并返回响应 然后我想检查视图组件中是否没有错误,那么它将是 200 如果视图组件有错误,那么它将是任何其他错误代码。
下面是 ViewComponent 代码
protected IViewComponentResult TestViewComponent()
{
try
{
// Do code here
}
Catch(Exception ex){
return View(viewName)
}
return View(viewName);
}
我想在 catch 块中设置状态码。
请帮帮我。
提前谢谢你。
【问题讨论】:
标签: asp.net-core