Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get my Web API app to run again after upgrading to MVC 5 and Web API 2?

I upgraded my Web API app to the funkelnagelneu versions using this guidance:

http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2

However, after going through the steps (it seems all this should be automated, anyway), I tried to run it and got, "A project with an Output Type of Class Library cannot be started directly"

What in Sam Hills Brothers Coffee is going on here? Who said this was a class library?

So I opened Project > Properties, and changed it (it was marked as "Class Library" for some reason - it either wasn't yesterday, or was and worked fine) to an Output Type of "Windows Application" ("Console Application" and "Class Library" being the only other options).

Now it won't compile, complaining: "Program 'c:\Platypus_Server_WebAPI\PlatypusServerWebAPI\PlatypusServerWebAPI\obj\Debug\PlatypusServerWebAPI.exe' does not contain a static 'Main' method suitable for an entry point..."

How can I get my Web API app back up and running in view of this quandary?

UPDATE

Looking in packages.config, two entries seem chin-scratch-worthy:

<package id="Microsoft.AspNet.Providers" version="1.2" targetFramework="net40" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" />

All the others target net451. Could this be the problem? Should I remove these packages?

UPDATE 2

I tried to uninstall the Microsoft.Web.Infrastructure package (its description leads me to believe I don't need it; also, it has no dependencies) via the NuGet package manager, but it tells me, "NuGet failed to install or uninstall the selected package in the following project(s). [mine]"

UPDATE 3

I went through the steps in again, and found that I had missed one step. I had to change this entry in the Application web.config File :

<dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>

(from "4.0.0.0" to "5.0.0.0")

...but I still get the same result - it rebuilds/compiles, but won't run, with "A project with an Output Type of Class Library cannot be started directly"

UPDATE 4

Thinking about the err msg, that it can't directly open a class library, I thought, "Sure you can't/won't -- this is a web app, not a project. So I followed a hunch, closed the project, and reopened it as a website (instead of reopening a project).

That has gotten me further, at least; now I see a YSOD:

Could not load file or assembly 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

UPDATE 5

Note: The project is now (after being opened as a web site) named "localhost_48614"

And...there is no longer a "References" folder...?!?!?

As to that YSOD I'm getting, the official instructions (http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2) said to do this, and I quote:

"Update all elements that contain “System.Web.WebPages.Razor” from version “2.0.0.0” to
version“3.0.0.0”
."

UPDATE 6

When I select Tools > Library Package Manager > Manage NuGet Packages for Solution now, I get, "Operation failed. Unable to locate the solution directory. Please ensure that the solution has been saved."

So I save it, and it saves it with this funky new name (C:\Users\clay\Documents\Visual Studio 2013\Projects\localhost_48614\localhost_48614.sln)

I get the Yellow Strip of Enlightenment across the top of the NuGet Package Manager telling me, "Some NuGet packages are missing from this solution. Click to restore from your online package sources."

I do (click the "Restore" button, that is), and it downloads the missing packages ... I end up with the 30 packages.

I try to run the app/site again, and ... the erstwhile YSOD becomes a compilation error:

The pre-application start initialization method Start on type System.Web.Mvc.PreApplicationStartCode threw an exception with the following error message: Could not load file or assembly 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..

Argghhhh!!! (and it's not even talk-like-a-pirate day).

UPDATE 7

I'm still curious about why Humpty Dumpty fell down and broke his crown (so to speak), but I finally threw in the towel and created a new project, copying-and-pasting code - and it works fine (VS 2013 and Web API, with REST methods called from a VS 2008, .NET 3.5 client app).

like image 805
B. Clay Shannon-B. Crow Raven Avatar asked Oct 18 '13 18:10

B. Clay Shannon-B. Crow Raven


2 Answers

In the last step on the migration guide did you delete only the MVC4 Project Guid that is specified? I had the same problem as you and when I retraced my steps realised that I had deleted all three of the Guids within in my .csproj file. I restored the other Guids within this element and the solution now recognises it as a MVC application and now works OK. So the line now reads:

<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>

Hope that helps

like image 136
nomadoj Avatar answered Sep 28 '22 00:09

nomadoj


I recently ran into this problem, but it was a simple fix. I have multiple projects in my solutions (including several shared PCLs) and when I unloaded the project to change the ProjectTypeGuids, it set a PCL as the default. Right-clicking over my web service project and setting it back to the default startup project fixed it and everything is working.

like image 39
SpokaneDJ Avatar answered Sep 28 '22 01:09

SpokaneDJ