Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 在视图组件中设置响应状态代码_Asp.net Core - Fatal编程技术网

Asp.net core 在视图组件中设置响应状态代码

Asp.net core 在视图组件中设置响应状态代码,asp.net-core,Asp.net Core,我想在视图组件中设置状态代码,以便我可以在.ts文件中检查它 这是我的.ts文件代码 window.onload = function () { fetch('../Controller/ActionName', { headers: { RequestVerificationToken: (<HTMLInputElement>document.getElementById("Token")).valu

我想在视图组件中设置状态代码,以便我可以在.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/';
    }
};
我想在catch块中设置状态代码

请帮帮我

先谢谢你

无法作为HTTP端点直接访问。它们是从 您的代码(通常在视图中)。视图组件从不处理视图 请求

根据,组件从不处理请求,视图组件方法在签名上重载,而不是来自当前HTTP请求的任何细节。因此,无法直接返回视图组件中的状态代码。如果要返回视图组件内的自定义状态代码,则不可能

您应该修改代码,将检查逻辑移动到控制器操作中,而不是视图组件中,如下所示:

public IActionResult LoadVisit(int? id)
{

    if (id == null || id == 0)
    {
        return NotFound();
    }
    var model = new VisitViewModel();
    try
    {
        model = await visitAPI.GetVisit(clientID, visitID);
    }
    catch (Exception ex)
    {
        return StatusCode(500, "Error message");
    }

    return ViewComponent("ClientVisit", model });

}
但是,如果您直接在视图组件内部抛出异常,客户端将收到500个错误,而不是200个错误

如下面的测试演示:

public class PriorityListViewComponent : ViewComponent
{
    private readonly ToDoContext db;

    public PriorityListViewComponent(ToDoContext context)
    {
        db = context;
    }

    private Task<List<TodoItem>> GetItemsAsync(int maxPriority, bool isDone)
    {
        return db.ToDo.Where(x => x.IsDone == isDone &&
                             x.Priority <= maxPriority).ToListAsync();
    }
    #region snippet1
    public async Task<IViewComponentResult> InvokeAsync(
        int maxPriority, bool isDone)
    {
        string MyView = "Default";
        // If asking for all completed tasks, render with the "PVC" view.
        if (maxPriority > 3 && isDone == true)
        {
            MyView = "PVC";
        }
        try
        {
            var items = await GetItemsAsync(maxPriority, isDone);
            return View(MyView, items);
        }
        catch (Exception)
        {
            throw;
        }

    }
    #endregion
}
公共类优先级ListViewComponent:ViewComponent
{
私有只读ToDoContext数据库;
public PriorityListViewComponent(ToDoContext上下文)
{
db=上下文;
}
私有任务getItemsAsSync(int-maxPriority,bool-isDone)
{
返回db.ToDo.Where(x=>x.IsDone==IsDone&&
x、 优先级3&&isDone==true)
{
MyView=“PVC”;
}
尝试
{
var items=await-GetItemsAsync(maxPriority,isDone);
返回视图(MyView,items);
}
捕获(例外)
{
投掷;
}
}
#端区
}
无法作为HTTP端点直接访问。它们是从 您的代码(通常在视图中)。视图组件从不处理视图 请求

根据,组件从不处理请求,视图组件方法在签名上重载,而不是来自当前HTTP请求的任何细节。因此,无法直接返回视图组件中的状态代码。如果要返回视图组件内的自定义状态代码,则不可能

您应该修改代码,将检查逻辑移动到控制器操作中,而不是视图组件中,如下所示:

public IActionResult LoadVisit(int? id)
{

    if (id == null || id == 0)
    {
        return NotFound();
    }
    var model = new VisitViewModel();
    try
    {
        model = await visitAPI.GetVisit(clientID, visitID);
    }
    catch (Exception ex)
    {
        return StatusCode(500, "Error message");
    }

    return ViewComponent("ClientVisit", model });

}
但是,如果您直接在视图组件内部抛出异常,客户端将收到500个错误,而不是200个错误

如下面的测试演示:

public class PriorityListViewComponent : ViewComponent
{
    private readonly ToDoContext db;

    public PriorityListViewComponent(ToDoContext context)
    {
        db = context;
    }

    private Task<List<TodoItem>> GetItemsAsync(int maxPriority, bool isDone)
    {
        return db.ToDo.Where(x => x.IsDone == isDone &&
                             x.Priority <= maxPriority).ToListAsync();
    }
    #region snippet1
    public async Task<IViewComponentResult> InvokeAsync(
        int maxPriority, bool isDone)
    {
        string MyView = "Default";
        // If asking for all completed tasks, render with the "PVC" view.
        if (maxPriority > 3 && isDone == true)
        {
            MyView = "PVC";
        }
        try
        {
            var items = await GetItemsAsync(maxPriority, isDone);
            return View(MyView, items);
        }
        catch (Exception)
        {
            throw;
        }

    }
    #endregion
}
公共类优先级ListViewComponent:ViewComponent
{
私有只读ToDoContext数据库;
public PriorityListViewComponent(ToDoContext上下文)
{
db=上下文;
}
私有任务getItemsAsSync(int-maxPriority,bool-isDone)
{
返回db.ToDo.Where(x=>x.IsDone==IsDone&&
x、 优先级3&&isDone==true)
{
MyView=“PVC”;
}
尝试
{
var items=await-GetItemsAsync(maxPriority,isDone);
返回视图(MyView,items);
}
捕获(例外)
{
投掷;
}
}
#端区
}

由于返回类型为IViewComponentResult,因此无法使用返回状态代码(500);据我所知,组件从不处理请求,视图组件方法在签名上重载,而不是来自当前HTTP请求的任何细节。因此,您应该将检查逻辑移出视图组件并移入控制器,然后在没有异常的情况下将结果数据传递到视图组件中。请参阅我的更新方法。我们将在控制器操作中调用ViewComponent。此外,如果您直接在ViewComponent代码中抛出错误,客户端将收到500个错误。由于返回类型为IViewComponentResult,因此无法使用返回状态代码(500);据我所知,组件从不处理请求,视图组件方法在签名上重载,而不是来自当前HTTP请求的任何细节。因此,您应该将检查逻辑移出视图组件并移入控制器,然后在没有异常的情况下将结果数据传递到视图组件中。请参阅我的更新方法。我们将在控制器操作中调用ViewComponent。此外,如果您直接在ViewComponent代码中抛出错误,客户端将收到500个错误。