Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

.net - Best way to call Api from Web Api Project

I have a .NET Core Project in which I need to create an API that will call another API(outside of this project) .
And I've got a problem , because I don't want to make external API call from a controller , because as I understand - controllers should be only called from the API and reroute data .
Task of my API is to authenticate and authorize a user and if he has rights for it - call external API .
I thought about creating a Service that will call external API , but I'm not sure that it would be good idea to keep this logic in same Web-API project.
What will be the best option to do this - create a library or something else to make external API call? I attached a scheme to show what I mean .
enter image description here

question from:https://stackoverflow.com/questions/65906865/best-way-to-call-api-from-web-api-project

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you can be pragmatic. Why is calling the External API directly from your API a problem? We are missing several of the details that could make this an interesting question. Also, might be better for the Software Engineering forum.

You have a few options:

  • Directly call the external API from the client. If there is additional server side processes that are needed maybe this isn't an option.

  • Directly call the external API from your API project.

A wrapper library just make another layer of indirection that I'm not sure you'd benefit from at this point. If you are worried about keeping the code clean, look at using something like Mediatr so that your controller classes aren't littered with code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...