【问题标题】:returning multiple objects from a function [C#],[ASP.NET Core]从函数 [C#]、[ASP.NET Core] 返回多个对象
【发布时间】:2021-09-28 04:11:13
【问题描述】:

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

【问题讨论】:

  • 你能分享一下功能的成本以及它是如何使用的吗?

标签: c# asp.net azure asp.net-core web-applications


【解决方案1】:
[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 应检查响应消息,然后执行重定向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-09
    • 2022-08-07
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 2022-04-29
    • 1970-01-01
    相关资源
    最近更新 更多