Which is better for building an API for my website

2020-07-16 12:17发布

I have a website built with MVC, and now I want to build an API for this website, to let users to use this API to implement different website, web services, plugins and browser extensions.

I went through this article but didn't get yet which to use.

General info about the API I want to build:
The user of the API will have a key user name and password to be able to use the API.
API will let users add content to my DB after validating this data.
API will let users upload images to my server.
API need to have friendly URLs.

Which technology will fit in my case?

Also will help me decide is to know what is the technology behind stackoverflow API?

3条回答
冷血范
2楼-- · 2020-07-16 12:47

Have you considered instead using OpenRasta?

Its clean, ReSTful approach makes it ideally suited to developing web-based APIs in .NET.

查看更多
干净又极端
3楼-- · 2020-07-16 12:47

Unless you have a very compelling reason to use WCF, why not just expose your data through the ASP.NET MVC site. You can easily return JSON or any other non-HTML data format that suits your needs from within ASP.NET MVC.

Depending on how similar the interface between your web site and your API need to be you might be able to use your existing controllers/actions or create new ones. In theory if the HTTP request indicates "application/json" you can just return JSON to those requests and be done...but in practice you will probably need to tweak the end points (controllers/actions) to make them friendly to your new consumer (other developers targeting your API)

WCF is very good and feature rich but not everybody is familiar with it. You might incur a learning curve getting a good understanding of it. Again, if there is a very valid reason for it then it's worth it, but from what you are describing you will probably be just as good with plain ASP.NET MVC.

查看更多
欢心
4楼-- · 2020-07-16 12:55

Try WCF Web Api. It has got all the cool stuff to create good RESTFul api, and it satisfies all your requirements.

  • You will use basic or some custom http authorization, simple as that.
  • You will expose very basic urls for creating data, validate and return response, trivial.
  • RESTFul means friendly url -s.

The general idea is that this library gives you strong manipulation options from the very start of request till its response back. This is the main HTTP api for .NET Framework and WCF and it continues its evolution.

查看更多
登录 后发表回答