Reputation: 337
I've done quite a lot of research on this topic but can't seem to find an answer.
I have written an ASP.Net Core 2 Web API. I am now wanting to develop a front end. I have been looking at using ASP.Net Core 2 Web App using razor, however I am struggling to call my web API. All the ASP.NET guides seem to include database access in the web apps controller rather than calling an external web api.
I have seen some example of using a HTTP client in the Razor controller to call the web API but I'm not sure if this is best practice.
My end goal is to have two applications a web api and a web app. I don't mind learning new technologies but I have started using Razor as it seems quite simple.
I have also thought about following the ASP.NET Core guides and not have an API but this doesn't seem like best practice either.
Upvotes: 2
Views: 5215
Reputation: 1940
Unless the two applications will be deployed separately, then you probably shouldn't be using an HttpClient
in your MVC(Razor) controllers.
For your front-end, you need to decide whether you want to stick exclusively to Razor, or use a client-side framework such as Angular or React. If you're using Razor, then you are effectively running your client from server-generated files. In which case you may as well abandon WebApi and serve your data directly (i.e classic MVC application).
If you want to stick with WebApi, then ideally you will need to make calls to it from JavaScript. Most of the big frameworks/libraries can do this. Let me know if you'd like an example (and which library), and I'll expand on this answer.
Upvotes: 2
Reputation: 303
You seem to be still debating whether or not to have an API, which makes me think you're not yet 100% clear on the solution to whatever problem you're trying to solve. I'd say answering this is probably the first step.
With regards to Razor; I don't know what a razor controller is. It depends on what your front end is, but assuming it's a website (sounds like it is) then my personal preference would be to have a Razor front end, with AJAX calls to the API.
You might take this a stage further and look to separate your back end and your front end. If you have an API then you can think of it as a separate application to be called, in which case you're not limited to the front end technologies you use.
I hope this is helpful.
Upvotes: 1