Adding API controller in same project as MVC controllers - asp.net-mvc

How can an API controller be added to a project with existing MVC (non-API) controllers?
Problem:
I've created a very basic "hello world" MVC project with only one model, view, and controller. When I attempt to add the API controller (not using scaffolding), I can't get using System.Web.Http; to show in IntelliSense. using System.Web will show up, but the .Http part is not available. What do I need to do or change?
Details:
I'm using VS2013 Express on a Win7 machine.

David Tansey pointed in the right direction with his comments, but after researching there's more to this than I realized as a beginner.
First, this wasn't available in the using statement because I didn't have a reference for system.web.http in my project. Digging further, I found the reference wasn't even available! So, I had to install using NuGet Package Manager Console Powershell.
In powershell, I can't just do install-package System.Web.Http. That package doesn't exist. But, thanks to one answer that David Tansey's comments pointed to, I noticed I could do install-package microsoft.aspnet.webapi. Wonderful! Now System.Web.Http is listed under References in my project, and I can write the using statement in my API controller code: using System.Web.Http;
For a beginner like me, a lot of steps to go through, and a lot of material to comprehend, to get started.

Related

Asp.net V5 Web API fails to add reference to AspNet.Identity

I have created a new Web Api project in VS2015 community Edition using framework 4.6
This new project has no extra references other than the ones that were added by default. When I attempt to add a reference to Microsoft.AspNet.Identity I get the following error
Strangely enough, I have another Web Api project in the same solution to which I have added the same reference and it works. I can't seem to figure out what is going on here. Please help.
just try install Microsoft.AspNet.Identity.Framework via NuGet Manager.

Issue with Visual Studio 2013, MVC 4.0 application and Web API 2.2 Nuget Package Install

I have a standard MVC 4.0 project for my application which was working correctly. I needed to use WebAPI within the same project so I installed the Nuget Package for WebAPI 2.2.
All seemed well and WebAPI ran fine after some tests, and my project also ran fine, except for the fact that now I cannot add standard MVC controllers, they all refer to adding a WebAPI controller and also options to add a standard view from an action inside a standard MVC controller using the Right Click context menu are now missing.
It seems as if it is assuming it is a WebAPI project only now, however I still need all MVC related options to be present. I am using Visual Studio 2013.
Any advice on this appreciated.
I believe it is not possible to use both WebAPI and MVC in the same project.
I managed to get it pretty much working using two projects in the same solution e.g. MyProject.MVC project and MyProject.API. However this turned out to be a bit clumsy, the biggest issue for me was getting Unity to work for both cases. Eventually I got that working too, but after a few days I figured I didn't actually need the WebAPI portion and it was causing more work than it was worth, so I removed that from the solution.
Not sure this answers your question tbh, but hopefully useful in some way. Btw in the next version of MVC6 (vNext) they are apparently planning to combine the Mvc and WebApi parts into a single framework, so this should ease the pain!

Did VS2013 incorrectly upgrade my project files?

I had a solution that included both an ASP.NET MVC web project and a WebAPI project. The solution was created originally with VS2012, but I recently upgraded it to VS2013.
I just noticed when going to add a controller that I'm not getting the correct options in the right click menu. For instance if I go to the web project and right click the controllers folder, the option to add a controller isn't there. Instead I have add WebAPI controller. It's exactly the opposite for the WebAPI project.
Things seem to build and run ok so far, but it's going to be a pain manually adding things and I'm wondering if something else might break. Any idea why this is happening or how to fix it?
I would guess that project type is different in your csproj. Take a look at this question:
What is the significance of ProjectTypeGuids tag in the visual studio project file
Here is what I have on my machine for c# asp.net mvc project:
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
Take a look at your guids and see what they mean.
You could see the meaning of the different GUID in the register :
•HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\10.0\Projects for
ProjectTypeGuids
•HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\10.0\Packages for
packages reference by some project

Monodevelop MVC3 razor - what is the trick to get it to work?

I use monodevelop 3.0.4.7 and mono 2.11.4 on mac OS 10.8.
When I create a new solution from scratch in monodevelop I can create several types of ASP.NET mvc3 projects with razor views, but the projects get created with references to system.web.helpers and system.web.webpages that are "not available for Mono/.net 4.0 in mono 2.11.4" and won't build.
If I change my .net version to 4.5 in the project options I get the system.web.webpages reference resolved, and if I simply remove the reference to system.web.helpers I am able to build the default site. However, it is unable to make use of .cshtml views, it only looks for aspx views.
I have tried different things related to references and versions of mono, changing the viewengine to razor in global.asax.cs and even copied dll's from a windows-based asp.net mvc application, but something new breaks all the time.
It seems strange that there are project types included that don't work from scratch, so I'm guessing/hoping I'm missing something much simpler?
I was sitting with the exact same problem, on Linux though, so I decided to ask on the mono mailing list (see the answer to my question here: http://lists.ximian.com/pipermail/mono-list/2012-October/049022.html).
The short answer is: no, you're not missing anything simple.
Long version: The current version of mono is not compiling the System.Web.Helpers.dll, so you have to do some footwork your self, to get it compiled. That is described in the link above.
I guess we just have to wait a little longer before this becomes part of the mono framework :/
I happened across a solution for this issue here:
http://blog.miraclespain.com/archive/2013/Jun-24.html
Basically in xamarin studio you can add a version of nuget from the following add-in repository source:
http://mrward.github.com/monodevelop-nuget-addin-repository/4.0/main.mrep
Once nuget is installed, you can create a new mvc3 razor project, notice the missing references for system.web.helpers and system.web.pages and just remove them, then use nuget to add the microsoft aspnet web pages 2 package which solves the missing references.

the command exited with code 4 with EntityFramework.SqlServerCompact nuget packge

I am having an issue when I try to use 'EntityFramework.SqlServerCompact nuget packge'.
I have created my model classes and I am also using the new ASP.NET MVC Tools Update MVcScaffolding out for the box feature when creating a new controller. this tooling feature also creates my DbContext class.
as it is explaind here :
http://www.hanselman.com/blog/Mix11WebPlatformAndToolsKeynoteDemoScript.aspx
Everything is fine. But, when I add the 'EntityFramework.SqlServerCompact nuget packge' and try to compile, I am getting the following error;
My machine is windows vista, 32bit and I am using VS 2010 pro if it matters.
I shared the link of this question on Scott Hanselman's blog post as comment and he gets back to me in an hour or so. the answer is as follows;
Perhaps a character in your path? Does
it work in a folder like c:\dev?
and it totally worked. the problem related to folder chars and I tried it inside the c:\apps folder. thanks Scott !

Resources