Debugging SharePoint – Web application could not be found Issue

Sometimes when I debug my SharePoint code from Visual Studio, I get the exception:

System.IO.FileNotFoundException was unhandled
Message=The Web application at https://jl-sp2010/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.
Source=Microsoft.SharePoint
StackTrace:
at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
at Microsoft.SharePoint.SPSite..ctor(String requestUrl)
at SPConsoleApplication_SPRouterRule.Program.Main(String[] args) in C:\Install\SPConsoleApplication_SPInstall.Solution\SPConsoleApplication_SPRouterRule\Program.cs:line 33
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Most often this is due to one of three things:

  • Must run as administrator
  • Wrong target framework
  • Wrong target application

 

Run as administrator

The Visual Studio instance must be started as “Run as administrator”.

image

Target framework: .NET Framework 3.5

Visual studio creates a new console application with the setting  .NET 4.0 . For SharePoint you need to set it to .NET Framework 3.5

image

Platform target: Any CPU

Visual studio creates a new console application with the setting  x.86 . For SharePoint you need to set it to Any CPU

image