c# - 从函数 [C#]、[ASP.NET Core] 返回多个对象
问题描述
我有一个功能可以检查用户是否分配了正确的角色。因此,如果用户没有正确的角色,我将返回 Forbid(),以便将其重定向到登录页面。但现在我想要一个 toster 消息显示适当的消息,为什么它被重定向。所以我在寻找如何显示该 toast 消息并重定向到登录页面的方法。
解决方案
[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 应检查响应消息,然后执行重定向。
推荐阅读
- python-3.x - 如何从地理数据中获取城市
- docusignapi - Docusign 请求授权码相关查询
- laravel - 如何过滤laravel中的特定记录?
- javascript - 规范化后如何保留键索引对象的项目顺序(来自服务器)?
- css - 如何使用 React 组件避免 z-index 错误
- caching - 有没有办法在 NativeScript Angular 应用程序中禁用 WebView(iOS 和 Andriod)中的所有缓存?
- autodesk-forge - Autodesk forge viewer.js 从 6.6 更新到 7.1 findPropertyDbPath 返回 null
- reactjs - 在 React 中分离 axios api
- arrays - one-hot 编码问题
- python - 解决“从数据帧复制切片”的问题