dot net core web api project templates are different - asp.net-core

I just compared the code generated by dotnet new webapi and dotnet new angular, and checked only the web api code.
For some reason controllers derive from different classes, in project created with dotnet new webapi controller is derived from ControllerBase, while in project created with dotnet new angular controller is derived from Controller
Also the return types for actions are different, in angular template its the actual return type, while in webapi template its ActionResult
Why is that?
Which option is the "best"? And why are they different?

Before .NET Core Web API and MVC controllers were similar.
But starting with 2.0/2.1 Web API was changed.
Now you should derive class from ControllerBase instead Controller and use [ApiController] attribute.
Old style will continue to work anyway.
Angualar use Web API, I suppose, so template should be updated.
See WEb API documentation.

ControllerBase doesn't have any of the Razor/view support. ControllerBase is used for APIs.

Related

How to add views to ASP.NET Core Web API?

Controllers process requests and return views in ASP.NET MVC, but how to add presentation layer (views or html pages) in ASP.NET Core Web API? I need to show to the user not the IEnumerable collection of objects, but a real webpage. Sorry for the stupid question.
Add MVC packages if not available from nuget package manager
using Microsoft.AspNetCore.Mvc;
add this line to startup.cs
//replace => services.AddControllers();
services.AddControllersWithViews();
I hope you will be able to use MVC feature from now.

Web Application vs Web Api project types in Asp.net Core

I know that Asp.Net MVC and Asp.Net Web API were merged into one code in Asp.net Core and they inherit from Controller base class and can all return implementations of IActionResult. it be a View for MVC or Json for web api.
But when i want to create a Asp.net Core project, it offers two templates (Web Application and Web Api ), according to what i said in above, there is no differences between these controllers, why there is two templates? is there any differences that i don't know about it?
The web application template will create folders and import stuff needed for a web application such as jquery, css etc. Web api template will create folders and import stuff for a web api. Also the controllers created by default will have different implementations, for example, web application will be returning views and the views will be created in the appropriate folder.
So although they derive from the same controllers, each type of project requires different dependencies.
If I were you I would go ahead and create one for each type and see the difference.
If you want to have both web api and web application in the same project, use areas. This way your web and api will have separate controllers, folders and models. Also if you want to separate them in the future, it will be easy to do so.
The difference between 2 templates is-
The WebAPI template starts with a Controller class that will allow you to respond to RESTful requests at the /api/Values endpoint.
The Web Application template will give you an MVC framework enabled project with some Razor views, the bootstrap CSS framework and jQuery library installed.
If you want to create project with both MVC and API controllers then I would suggest to go with ASP.NET Core Web Application template and add require dependencies.

ASP.NET 5 , wanted to separate entity framework from Web Project

I'm trying to achieve below application structure
App.Model
-- UserModel
-- OrderModel
App.Service
-- UserService
-- IUserService
App.Entity
-- DbContext
Website
-- WebAPI
-- Invokes Service
Most of ASP.NET 5 application examples , initializes entity framework in startup file of website. like .AddEntityFrameowrk() .. etc.
Need to know how we can separate the Entity into separate project and how do we initiate the DBContext if we are invoking repository from Service project.( since startup file is not available for class library)
In previous projects, I have taken the approach of maintaining multiple ASP.NET projects in one solution (or multiple solutions). This will give you the ability to remove the EF dependencies from your web application and have the web application talk only to the API which handles all the EF
In the past, I've created a dedicated 'domain' project which contains all the models and can be referenced by both your web application and API projects.
So the project structure would look like this
Domain project: Contains only c# classes that represent your model. Start with an 'empty' project
API: Contains your web API. Uses EF to interact with database. I would personally scaffold the APIs fro your model but that is a personal choice. Right now, in ASP.NET core 1.0 RC1, the web api template is poor so I'd start with the full 'web application' one and remove all the default views, controllers, scripts etc. References the domain project so that you can scaffold your API controllers from the model
Web Application: Your main web application project. Start with the 'web application' template but use HttpClient I your controller to use the api for data interactions. In the past I've created a set of static 'api helper classes' to make this task easier but that is a personal style choice again. References the domain project so that you can convert incoming JSON responses to the full .net model

Using WebApi and structure map dependency injection

Although there are so many question on stack overflow which tends similar to my this question but no one is resolving my problem
I was using an MVC4 internet application wherein i had few MVC controller and for dependency injection i was using Structure map. Although dependency injection works fine with MVC controller but when i added an WebApi controller in the same MVC internet application and using the same parameter in constructor of WebApi controller as what i am using in MVC controller but dependency injection is not working with WebApi controller, although if i don't use dependency injection for WebApi controller(parameterless constructor), then it works fine, but for WebApi dependency injection(parameterized constructor) it is throwing an error No parameter less constructor is found.
Conclusion depedencies are not being injected for WebApi Controller in Internet(MVC application).
Few articles suggested to use DependencyResolver.SetResolver(). i used but did not resolve the issue.
The reason why WebApi controller were not working is as following:
Since MVC Controller uses different DependenyResolver instance that is the part of System.Web.MVC .dll and within the System.Web.MVC namespace
http://msdn.microsoft.com/en-us/library/system.web.mvc.idependencyresolver(v=vs.98).aspx
Where as Api Controllers uses DependencyResolver instance that is part of System.Web.Http.
http://msdn.microsoft.com/en-us/library/system.web.http.dependencies.idependencyresolver(v=vs.108).aspx
MVC and WebAPI controllers have a different way of setting their dependency resolver. This is how I set my dependency resolver for Unity:
public void ConfigureForMvc4()
{
DependencyResolver.SetResolver(
new UnityMvc4.UnityDependencyResolver(Container));
}
public void ConfigureForWebApi()
{
GlobalConfiguration.Configuration.DependencyResolver =
new UnityWebApi.UnityDependencyResolver(Container);
}
You need to add Dependency Injection files for WebApi
Install NuGet StructureMap.WebApi2 and in the App_Start/WebApiConfig.cs file, call StructuremapWebApi.Start();
Refer: https://www.exceptionnotfound.net/setting-up-dependency-injection-in-web-api-with-structuremap/

webApi vs SPA .NET MVC 4 controller

So I'm about to create my first SPA project using .NET MVC4. But I just need a little clarification before I start. It seems from a few tutorials, SPA is built based on WebAPI architecture.
Is it safe to say SPA = WebAPI + knockout.js + history.js +
upshot.js which all together function as a web application that can
run somewhat offline? so later down the road if we want to build a
native phone app, we can always just call it from the SPA
DataServiceController?
What's the difference between webAPI controller
(that inherits from ApiController) VS SPA controller (that
inherits from DbDataController<..>)
SPA works with WebAPI, history.js, upshot.js. Knockout is not necessarily required (i.e. you can choose your own client-side framework. From what I understand, you need to use Entity Framework as well.
DbDataController is a generic type that requires specifying the DataContext you want to expose. This is not required with ApiController. It seems that there SPA requires using EF, which seems restrictive in my opinion.