If I build a web API project in Asp.Net Core 1.x and I want to create an interface in the same project or solution, is it possible to create interface pages and use Razor with Visual Studio Intellisense?
The application is built on an API for public consumption, but since my application will use the same data, I thought it would make sense to consume my own API instead of creating separate methods or constructs for calling APIs against โregularโ MVC (call the controller, get the data models, return the view). The client will have its own interface and receive data. I will have my own interface, but I want it to integrate into the same VS solution.
One drawback is that I lose my Intellisense because I create it around consuming JSON returning from the API. I understand that an API is a data return, not a Views. I am trying to get the best of all worlds and be more productive with Visual Studio features.
Everything I read is older. This and the older question. I read a lot about returning a View with an API, but I'm not sure I want this. I want to make as a regular API project not an API and an API project at the same time.
Is it possible?
I understand that this is not so, because the whole point of Razor is that it runs on the server, and the API is for clients outside of your application. In other words, I will need to create a controller called my API controllers, so I get Intellisense, which seems a little redundant and more difficult on the server.
Some features:
Creating a REST API using ASP.NET Core Syntax with Razor Syntax
ASP.NEt MVC using web API to return Razor view
asp.net mvc consumes asp.net web api endpoint
EDIT: This seems logical, https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/areas
EDIT: Here is what I did using this as a guide:
https://msdn.microsoft.com/en-us/magazine/mt763233.aspx
I created an area in my project. Under this, I created the name of the area, and under this I created the controllers, views (and under this House).
In my Startup.cs file, I added
app.UseMvc(routes => { routes.MapRoute(name: "areaRoute", template: "{area:exists}/{controller=Home}/{action=Index}"); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}"); });
Localhost url: port / AreaName
Inside my controller, I was able to reuse services from my service level, the abstractions that I solved earlier in the project.
namespace books.Areas.Controllers { [Area("Books")] public class HomeController : Controller { private readonly AppSettings _appSettings; public HomeController(Microsoft.Extensions.Options.IOptions<AppSettings> appSettings) { _appSettings = appSettings.Value; }
This is the same level of service that my API controller uses. Service Level (I use Dapper)
public List<books> GetAll() { using (IDbConnection db = new SqlConnection(_connectionString)) { string SqlString = "SELECT TOP 5 last_name, first_name FROM books"; var myBooks = (List<books>)db.Query<books>(SqlString); return myBooks ; } }
Once I was able to get my types, I was able to use Razor in my index in my region:
@model System.Collections.Generic.List<books> @{ } <html> <body> @foreach(var person in Model) { <ul><li>@person.last_name</li></ul> } </body> </html>
Without modification, I can still use my api / values โโURL to access my API, and I can refresh part of my page after this initial server rendering by calling the API via Ajax.