简体   繁体   中英

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. I was planning to save the api end point in the DB table and based on the user request, read and get this uri. But I'm not sure how to access the request object assocaited with an api end point from a different api controller.

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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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