首页 > 解决方案 > 从函数 [C#]、[ASP.NET Core] 返回多个对象

问题描述

我有一个功能可以检查用户是否分配了正确的角色。因此,如果用户没有正确的角色,我将返回 Forbid(),以便将其重定向到登录页面。但现在我想要一个 toster 消息显示适当的消息,为什么它被重定向。所以我在寻找如何显示该 toast 消息并重定向到登录页面的方法。

标签: c#asp.netazureasp.net-coreweb-applications

解决方案


[FunctionName("SampleResponse")]
public static async Task<IActionResult> SampleResponse(
    [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
    ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");
 
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
 
    string name = data.name; // get name from dynamic/JSON object
 
    if (name == null)
    {
        // Return a 400 bad request result to the client with additional information
        return new BadRequestObjectResult("Please pass a name in the request body");
    }
 
    // Return a 200 OK to the client with additional information
    return new OkObjectResult($"{name} exists");
}

您的 UI 应检查响应消息,然后执行重定向。


推荐阅读