Adding Web API to existing asp.net web forms Application - asp.net

My team wants to upgrade from WCF to Web API. We have a working asp.net web form application, that we have imported to VS2012 from VS2010. So far so good.
But now as I try to make a separate Web API project, I see that there is no Web API template available. The closest thing that I can find is by creating an MVC 4 application and setting the Project Template as WebAPI. I followed this way and everything falls in perfectly. I have the working API with a sample controller that I can invoke by making calls from the browser.
The only downside to this is that, this particular method brings in its own baggage. The MVC 4 project I created has JQUERY and other libraries included, plus some other folders that I probably don't need. What I want is the Web API structure only - and not the extra baggage.
I tried finding a template using online search but the package I found does not work properly and as very poor rating.
I hope I have illustrated my problem properly. I am looking forward for some feedback now :) Thanks.

In Visual Studio 2013
Right-click on the ASP.NET Web Forms project.
Add -> Add Scaffolded Item... or New Scaffolded Item...
Under Installed/Common/MVC/Web API choose the scaffold type you wish
to use.
Follow the instructions for the scaffold template. For example, when you choose "Web API 2 Controller with read/write actions" or "Web API 2 Controller - Empty", you are prompted for a controller name
You will then need to move the recently created controller into the recently created Controllers folder.
Results
From what I can see, Visual Studio does the following:
"App_Start/WebApiConfig2.cs" is created.
Controllers folder is created.
Web.config is edited to add "handlers" element with content in "system.webServer".
The following references are added:
System.Net.Http
System.Net.Http.Formatting
System.Web.Extensions
System.Web.Http
System.Web.Http.WebHost
packages.config is updated to include:
"Microsoft.AspNet.WebApi"
"Microsoft.AspNet.WebApi.Client"
"Microsoft.AspNet.WebApi.Core"
"Microsoft.AspNet.WebApi.WebHost"
Notes
Subsequently, I recommend following the same steps, starting with right-clicking on the Controllers folder instead of the project. This will put the new controller in the Controllers folder instead of at the root level.
Readme from Visual Studio after following the above steps:
Visual Studio has added the full set of dependencies for ASP.NET Web API 2 to project 'RowersCode.Web'.
The Global.asax.cs file in the project may require additional changes to enable ASP.NET Web API.
Add the following namespace references:
using System.Web.Http;
using System.Web.Routing;
If the code does not already define an Application_Start method, add the following method:
protected void Application_Start()
{
}
Add the following lines to the beginning of the Application_Start method:
GlobalConfiguration.Configure(WebApiConfig2.Register);

After much research I have been able to come up with a solution to this problem. Let me illustrate my solution with respect to the Visual Studio Version.
VS 2012
As I mentioned in the question, there is no definite way to create the Web API project in VS2012. You are gonna have to do it by creating an MVC 4 application and setting the Project Template as WebAPI. Then once you have done this and you have your Web API functional, you can safely delete the extra baggage like the Jquery libraries and other stuff, because these things are absolutely of no use here in your project.
VS 2013
In VS2013 there is however a better approach followed to add and manage the Web API projects. Currently I am using VS2013 for the Web API and all things have fallen into place just as I wanted. Kindly see this link and you will get a better idea
http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
I hope this information will help all those new to Web API. Especially for those who want to upgrade to Web API or add Web API to existing projects.

Related

Why CRUD razor pages in ASP.net CORE does not work out-of-box after Scaffolding with Database-first approch?

in VS 2019 i have created an web-app like this:
After that i have:
Created a database, and added it in VS2019 "SQL server explorer" (so connection is fine)
Then Scaffolded according to the command in the lasted post here: https://github.com/dotnet/efcore/issues/19986, and i get my "class/model" as a result of this.
Then I add my CRUD pages like demonstrated in this video: https://www.youtube.com/watch?v=q2u1VY28Drs
And that is ok, since the CRUD pages are created (Exists in solution explorer).
Now when i start the app, and manouver to the new CRUD pages as shown i the last video link, it does not work out of the box, the page does not exist: No webpage was found for the web address: https://localhost:44335/V2/index. I dont understand why, but suspect it has something to do with routing.
Or, what could be the problem here?
tnx
Updated 19.10
I create a MCV app, and follow the steps 1&2 above, then i follwo this guide at the "Add model" step: **https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/?view=aspnetcore-3.1 ** and
then add this: using Microsoft.EntityFrameworkCore; in my startup file. I do not add using MvcMovie.Data;
And it still do not work, the documentation is lacking to be mild on the database first approach. What do i NEED to do from the step: "Add a Model" in the MVC guide? anyone?
Or, what could be the problem here?
The key of this issue is that the project in this video is in asp.net core razor page rather than asp.net core mvc.
When you create the project, in Create a new ASP.NET Core web application box ,you should choose the template of Web Application, you can have a look for official documents:
Then you can follow the video operation to create the crud pages.
If it is in the asp.net core mvc project, you need to create New scaffolded Item in the Controller folder and should select MVC Controller with views, using Entity Framework template, you can have a look for this.

Publish Web API controllers in separate files

I have a problem with publishing Web API in Visual Studio.
In VS, when I publish the Web API, it is published as dll's, and when we have several controllers it is going to aggregate those into one dll.
But I have project that can have different controllers, like:
Android.cs
CoWorker.cs
TelegramBot.cs
and when I publish the project, I would like to publish the controllers in separated DLL's, because I should have update the controllers continuously and when I have an error in a specific controller, I can't publish another.
Please help!
What you are looking for will not happen on its own. If you want a plug and play like architecture where you can just publish only a module without re-publishing the whole solution.
To achieve this you need to move your controller(s)/module as a separate project (separate library). One of the useful configuration feature of ASP.NET Web API is that it allows you to define about the assemblies into which it will look to discover your controller types.
This is very useful if you have external assemblies and are not part of the Web API project.
You can implement interface System.Web.Http.Dispatcher.IAssembliesResolver GetAssemblies() or even can extend the DefaultAssembliesResolver as following.
public class CustomAssembliesResolver : DefaultAssembliesResolver
{
public override ICollection<Assembly> GetAssemblies()
{
//Your implementation here
}
}
You can find more details on this on following link.
MSDN
Sample
I think what you're looking for is a MICRO-SERVICE architecture ...
https://learn.microsoft.com/en-us/dotnet/standard/microservices-architecture/multi-container-microservice-net-applications/data-driven-crud-microservice
https://github.com/dotnet-architecture/eShopOnContainers
you can use .NET full framework
or the new .NET Core (it depends on what you have to do)

Create Controller and add Views to another project

Visual Studio 2015 + all updates.
Asp .Net Web application (MVC).
I start off by adding a few class libraries and separating the Asp .Net WA into layers i.e. DataAccess, Business Logic and the web project itself.
Once separated I add relevant references and everything is working as I expect it to be (i.e. the application functions as it did before I separated it into layers).
In my BL (Controllers are found here). I don't have the option to Add a Controller, like you would when right clicking the Controllers folder in the default project, so add the below line
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
to the csproj file for my class library and the Add Controller option now appears. Create a controller but throws an error which was due to not having a web.config file - add this file and all works (although it would be nice to have this library working without a web.config file).
The problem I've hit is, when the Controller is created it also adds a View template within the class library but my Views folder is located in my web project.
Have I done this wrong? Is there a workaround so when a controller is created, it also creates the Views into the correct project? Or another approach for this?
This is just a guess, but it seems like you are try to use a UI-based architectural pattern to build your business layer.
Typically, your models, views, and controllers are all maintained in the main web-app project. Any supporting functions (like your BL and DL) are added via class libraries. The Visual Studio MVC templates are built around that concept, which is why you had to manually add support with the GUID - and why it automatically creates the view.
If I may ask, why are you trying to build controllers into your BL? If you are trying to decouple your UI from your server code, perhaps WebAPI would be a better option.
UPDATE - A few helpful links
ProDinner - ASP.NET MVC Sample App
N Layered App with Entity Framework, Autofac, ASP.NET MVC and Unit Testing
Architecture Guide: ASP.NET MVC Framework + N-tier + Entity Framework and Many More
Most of your issues boil down to using the scaffold. The scaffold is great when you're just starting out or for extremely simple projects, but it quickly falls down beyond that. Specifically, adding a controller via scaffold is designed for an MVC project, so it expects to find things you'd find in an MVC project. Additionally, it creates scaffolded views in an appropriate directory in Views because, again, that's what it's designed to do.
The simplest solution, then, is to just not use the scaffolds. A controller is just a class that inherits from Controller. Nothing special there. Then, you can create the views where you want to create them.

can I add the Web API folders to an existing project?

Microsoft has a great tutorial here for learning how to build Web API web apps in Visual Studio 2013:
http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
in it, he shows you how to include the Web API folders and references:
...is there a way to add these to a previous ASP.NET Webforms project that didnt have that checkbox selected? I'm working on an existing app and want to add Web API functionality to it.
thanks
yes -- there is a tutorial for adding Web API to an existing ASP.NET Webforms project here:
http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-aspnet-web-forms
...it doesn't create the Models, Controllers, or App_Start folders, but i added them manually and placed my files from another stand-alone project there. once adjusting the namespaces it all operates properly.

Named Pipes references not working with ASP.NET MVC 4

I'm working on a API and using ASP.NET Web API in order to expose it. I tried to add a service reference to a Named Piped service that is responsible for authentication and authorization, but I couldn't reference it on my code.
That Named Pipe service is working properly and is used on a wide variety of projects. That is the first time we try to use it with ASP.NET MVC 4. It's working very well on another project that uses ASP.NET MVC 3.
Like you could see on the following image, I'm able to add the service reference. But It didn't appear on Object Browser or auto-complete!
When I try to invoke that service, it just does not show up anywhere.
It appears to be a problem with ASP.NET MVC 4 projects and Named Pipes service references.
I really don't know if that is a well know / documented behavior, but I wasn't able to find anything useful related to that question on the internet.
I could add a reference to that service using Visual Studio 2012 on any kind of project, except ASP.NET MVC 4 ones. The problem is specifically related to ASP.NET MVC 4 projects.
I tried to add the reference using Visual Studio 2010 and I had the same problem: working pretty well with ASP.NET MVC 3 and any other kind of project. Again, the problem is specifically related to ASP.NET MVC 4 projects.
Is anyone having the same problem? How could I go through it?
Since it appears to be a Visual Studio / ASP.NET MVC bug I just found an workaround.
I just figure out that I could use svcutil command line tool in order to generate the code for service client and data structures. Here are the syntax for the command:
> svcutil net.pipe://my_sevice/mex /namespace:"*, MyNamespace"
It generated the following files:
SecurityService.cs
output.config
Then I just imported the generated SecurityService.cs file into my current project and did a merge of the binding configurations available on the generated output.config with my own web.config.

Resources