2

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

my concern is that removing the web api and serving data directly is bad practice, a lot of articles I have read state that you should always write an API (I might be completely wrong here). Basically my aim is to write a web site that interacts with a database, whether thats via a web api or not, it doesn't really matter, I'm just trying to work out the best practices. I'm a C++ developer and this is the first time I've done any web stuff so just trying to get to grips with everything! I was keen to use Razor purely because it looked simple and makes use of C#.
The general feeling in the community is to write 2 different applications. A WebApi and a client. The choice to use Razor or not is a case of whatever is simplest and easiest to maintain. In your case it seems like a separate MVC application would be the best pick. This uses Razor on the server to generate and deliver static client pages/partials. It is from these pages that you need to use JS to make WebApi calls. In short, Razor can't do this for you, as it needs to run in the browser.
You could just write an MVC application, and develop your back-end services as if you were writing a WebApi on top of them (but not). Basically, keep all your logic and data in a different project. I think that would be my approach. It's a single application/solution, and will keep you focused on the front-end.
Ah OK, I think that's starting to make sense. So if I went with an API, I could still use Razor for the UI but then some other library (JS) to call the web api to retrieve the data? When you talk about the other option of just writing an MVC app, do you mean that all the database interaction would be within the app rather than getting a web api to do it? Thanks :)
Yes indeed - The MVC app would just interact with the DB directly (ideally via some dedicated service classes). This would eliminate the need for an API.
|
1

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.

2 Comments

thanks for the reply. I am still debating whether to use an API or not. I will only be developing a website front end so personally I don't see the need for an API but there is a lot of information suggesting that you should always develop an API which is why I was going down that route. If I go down the web api route then it would be a separate application so I guess my main question is how I call the web api from as ASP.NET Core Web App.
I'm not sure where you're getting your info from but there is no truth to the statement 'you should always develop and API'. You should not 'always' do any one thing, but instead figure out the best approach for your specific problem. An API is best generally used for when you want to separate the back and front ends; think of an API as a service which you call to run functions, get back data etc. By reading the other answers, it does sound like a simple MVC website might be your best approach indeed. Best of luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.