can I add the Web API folders to an existing project? - asp.net

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.

Related

Web API project template in VS

Why can't we create WebAPI project directly just like "WCF Service Application" template in VS.I want to create separate solution not using MVC or ASP.Net WebForms template. In our project we intend to have service layer on different physical machine hosted by IIS to be consumed by different web applications.
The ASP.NET team is in the process of making their offering more cohesive, an effort they call "One ASP.NET." Under One ASP.NET, MVC, Web Forms, Web API, SignalR, and Entity Framework are all first class citizens. They want to make it easy to create web applications that utilize these technologies without having to give up the others; that is, you can make an MVC project but still use Web Forms; or a Web Forms project can easily add a Web API Project.
Anyway, back to your question: you can't create a Web API project directly (i.e. it's not in the main project template list) anymore because it has been rolled into the the single ASP.NET project type.
They explain,
Starting with Visual Studio 2013, the guessing game about which
project type to choose is over. There is now only one web project type
in Visual Studio.
As you can see from the list of templates, we can choose to start with
a standard Web Forms, MVC, or Web API project type. The other project
types from the old MVC template dialog are still here. The interesting
part is the checkboxes underneath the list of templates. Here, we can
choose to add Web Forms references and folders to an MVC project, or
MVC references and folders to a Web Forms project. This is the gateway
to using more of these features in concert with each other in your
project.
Read the full MSDN blog article about One ASP.NET and the new project types here
You can, however, still create a project (or solution) that contains just the Web API references. Just use the ASP.NET project dialog to choose the Web API project type, and make sure all the other check boxes are unchecked. You'll be able to make your project just for Web API, and then go host it on a dedicated box for your services layer. It's all still there, the UI just looks a little different.

Adding Web API to existing asp.net web forms Application

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.

Add Project File to Existing ASP.NET Web Site in TFS

I have an existing ASP.NET Web Site (targeting v4.5) in VS 2012, which is in source control and is used by several other developers. It does not have a project file associated with it in source control (we are using TFS). I am wanting to create a project file for this website and place it in source control.
Could someone give me step-by-step instructions on how to do this, without disrupting the other developers and without losing all previous change history?
Well you should convert your web site project to a web application project. It has quite a few steps, you can follow steps provided by MSDN.

Tool or Standard Procedure to add a different type of Visual Studio Project to an existing one?

I have an existing website coded in ASP.Net 4.0 Webforms, and would like to add an "ASP.Net Dynamic Data Entities Web Application" (project templates of VS2010 to my actual project):
So my question is, what are the steps to "merge" the 2 projects or integrate the "ASP.Net Dynamic Data Entities Web Application" to my actual webforms.
I think I must merge my Global.asax.cs files and web.config file + move rest of the files. Is that correct ?
I already read that post (that may be outdated?):
http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4WebFormsApplications.aspx
Thanks for your answers :)
(the original title of this post was: “ASP.Net Dynamic Data Entities Web Application” integration with an existing Webforms)
I've read Scott Hanselman's article which you've provided in your question and yes the same technique still applies today, here are a few steps/considerations which should help you along the way:
When you create the new ASP.NET Dynamic Data Entities Web Application make sure that it's the same .NET Framework version as your Web Forms project, to avoid version conflicts.
Remember to add a reference to all the required libraries that you've been using in the Web Forms project.
Now it's safe to copy all the necessary .aspx pages to the new site.
There is some automatically generated code inside the Global.asax of a Dynamic Data project which is used to configure ASP.NET routing, it may be useful later so instead of replacing it with your Global.asax use a tool like WinMerge to merge the two files.

Visual Studio 2010: convert website project to web application project?

I have an existing solution in VS2010. When I click the properties of my project I get the view as can be seen in image1.png:
When I create a new empty ASP.NET web application and click the properties of my project I get the view as can be seen in image2.png:
A huge difference.
Now I'm implementing this code here: http://wcf.codeplex.com/wikipage?title=Getting%20started:%20Building%20a%20simple%20web%20api
And that tutorial assumes I get a view as I've shown you in image2.png
In image1 and image2 I've also marked the menu items: "Website" and "Project"
This makes me assume that I've created a website application whereas I need something different (I'm thinking a web application project).
Not sure what to do now, I need to convert my current application to another thing, but to which type and how to do it?
I also came accoss this post: http://blogs.msdn.com/b/webdevtools/archive/2009/10/29/converting-a-web-site-project-to-a-web-application-project.aspx
But before I start screwing up everything :P Is that what would solve my problem?
It should be possible to use either a web site or a web application project. In the example on the link you've posted, they use the predefined template for ASP.Net MVC Web Applications. This has several advantages - it sets up the structure and other resources for you - it's effectively a template.
For the purposes of the tutorial, why don't you just set up an application that way? If you've already written some code, you can just import it into the new application.
For future projects, you might want to consider which is best for your needs. Apart from structure, there are some other key differences between a web application and a web site project.
MSDN handily provides the differences here:
http://msdn.microsoft.com/en-us/library/dd547590.aspx
If you find you want the features of the web application, then the link you've posted is fine. You'll need to do some re-organizing of your references and so on and so forth, but the process is fairly straightforward (but not painless).
A "web site" in visual studio is the old, .net 1 style of creating web sites. It basically dynamically compiles the code in a folder when it's hit for the first time. Later .NET introduced a "web application" model where the code is compiled into a DLL and no code files are deployed with the site. "Web sites" are pretty much depcrated and shouldn't be used for any new projects unless you're doing a quick demo site.
The codeplex project you referenced also depends on MVC 3 (which are web applications).
You are probably better off just restarting like the tutorial says - create a new MVC application like it shows in its first screenshot.
If you do not have that option, use Web Platform Installer to add MVC for you - http://www.microsoft.com/web/downloads/platform.aspx
Good luck!
I can't tell exactly what's going on from your screenshots, but if you have a web site that you need to convert to a web application project, you can find the directions here
Although the instructions are specific to VS2005, they are basically the same for 2010 as well.

Resources