如何返回OkObjectResult和BadObjectResult - c# Azure函数 &g



我得到以下错误"不能隐式转换类型"Microsoft.AspNetCore.OkObjectResult">

当我尝试执行以下行时,显示<;microsoft . exe . bulk . accessibility . logging . accessibilityerroroutput

Microsoft.Omex.Bulk.Accessibility.Logging。AccessibilityErrorOutput是项目中的另一个类。但这门课没有用到它,我哪里做错了?

我想在函数成功或不成功执行时返回HTTP结果。

filePath ?? return new OkObjectResult("Accessibility Checker function executed successfully") : return new BadRequestObjectResult("Function did not execute successfully");

Azure函数API的完整代码:

namespace Microsoft.Omex.Bulk.Accessibility.Checker;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Extensions.Logging;
using Microsoft.Omex.Bulk.Accessibility.Logging;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
/// <summmary>
/// Azure function class to manage functional processing of Word document, Excel spreadsheet,
/// Powerpoint presentation to detect acccessibility issues.
/// </summary>
public class AccCheckerForOpenXML
{
private readonly ILogger<AccCheckerForOpenXML> logger;
public AccCheckerForOpenXML(ILogger<AccCheckerForOpenXML> log) => this.logger = log;
/// <summary>
/// Function that receives a httptrigger and then starts processing documents the Office Open XML files.
/// </summary>

[FunctionName("AccCheckerForOpenXML")]
[OpenApiOperation(operationId: "Run", tags: new[] { "docName" })]
[OpenApiParameter(name: "name", In = ParameterLocation.Query, Required = true, Type = typeof(string), Description = "The **Name** parameter")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Description = "The OK response")]
public async Task<AccessibilityErrorOutput> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req)
{
this.logger.LogInformation("AccCheckerForDocx: Run method:HTTP trigger --  function processed a request."); // 
string filePath = req.Query["name"]; // In the query will be a name
var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody); // If not the name will be in JSON
filePath ??= data?.name;
filePath ?? return new OkObjectResult("Accessibility Checker function executed successfully") : return new BadRequestObjectResult("Function did not execute successfully");
}
}

这是因为你的Task在这里定义了一个AccessibilityErrorOutput返回类型:

public async Task<AccessibilityErrorOutput> Run(...)] HttpRequest req)

如果你想使用OkObjectResult或其他结果,你必须指定你的函数返回一个任务,如下所示:

public async Task<IActionResult> Run(...)] HttpRequest req)

相关内容

  • 没有找到相关文章

最新更新