简体   繁体   English

如何在ASP.NET Web API中获取uri的请求对象?

[英]How to get the request object for uri in ASP.NET Web API?

I have a requirement within team where I need to create an api, which should provide the user with all available api and corresponding request for which the user requested. 我在团队中有一个需要创建api的需求,该API应该为用户提供所有可用的api和用户请求的相应请求。 I was planning to save the api end point in the DB table and based on the user request, read and get this uri. 我打算将api端点保存在数据库表中,并根据用户请求读取并获取此uri。 But I'm not sure how to access the request object assocaited with an api end point from a different api controller. 但是我不确定如何从其他api控制器访问与api端点关联的请求对象。

switch (service.ToUpper())
{
    case "MYFUNCTIONALITY":
        detail.endPoint = "api/myfunctionality/random";
        detail.requestObject = new AutoCreditCardTransactionRequest() { Quantity = 5, AcctmyIDs = myaccts };

        break;
    default: break;
}
return detail;

In the above code, I have hardcoded the end point and based on the case statement, the request class is also mentioned. 在上面的代码中,我对端点进行了硬编码,并基于case语句,还提到了请求类。 As I need to do this for all the end point, I wanted these end point to be moved to DB and then read from that, but issue for me is how to get the request class for the end point rather than specifying the request class 因为我需要对所有端点进行此操作,所以我希望将这些端点移至数据库,然后从中读取,但是对我来说,问题是如何获取端点的请求类而不是指定请求类

You can use ApiExplorer class's ApiDescriptions property to get ApiDescription for API endpoints then use ApiDescription.ParameterDescriptions property to access the input parameters for the API.. Something like following 您可以使用ApiExplorer类的ApiDescriptions属性获取API端点的ApiDescription,然后使用ApiDescription.ParameterDescriptions属性访问API的输入参数。

ApiParameterDescription paramDesc; // Use appropriate code to populate this 
var type = paramDesc.ParameterDescriptor.ParameterType;
var defaultValue = paramDesc.ParameterDescriptor.DefaultValue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM