Web Api calls return 404 on Server - asp.net-mvc

I'm aware this has been asked a lot of times on this site and others, but no solution i've seen so far is working, so here I am.
I have some web api routes set up in an mvc 4 project. Here's a sample:
routes.MapHttpRoute(name: null, routeTemplate: "Client/Accounts/Save", defaults: new { controller = "AccountsApi", action = "SaveAccount" });
Corresponding to the following controller and action:
public class AccountsApiController : ApiController
{
[HttpPost]
public object SaveAccount([FromBody] Account input)
{
.....
This works when running in the built in VS web server, and on a local (windows 7) installation of IIS7.5, with no fancy stuff done to it, but gives a 404 when running in IIS7 on Server 2008 R2.
Info:
MVC4 is installed on the server
The application is in an app pool with integrated mode and framework v4
What i've tried:
Played with routes enough to know it's not a routing issue
Have <modules runAllManagedModulesForAllRequests="true" /> set (is the default anyway)
Looked at this
Done the aspnet_regiis -ir dance
Custom errors off, compilation debug=false - if those make any difference.
There are only custom api routes, no catch all.
IIS Logs just indicate a 404, as you'd expect; nothing in system event logs
The api uses only GET and POST requests, neither of which are working. Might also be worth mentioning that the calls are all made via ajax.
system.web, system.webServer,runtime sections from config:
<system.web>
<compilation debug="false" targetFramework="4.0" />
<customErrors mode="Off" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Any suggestions hugely appreciated. Thanks.
Update
I reimplemented one of my controllers to use only HttpResponseMessage and HttpRequestMessage
[HttpGet]
public HttpResponseMessage GetAccount(HttpRequestMessage message)
{
message.CreateResponse(....
and again this works as expected when running locally (both cassini and full IIS local install on Win7), but fails when pushed onto the server.

Update: I just noticed that you said GET methods were not working either, in which case this advice is probably no use to you.
Try changing the signature of the method to
[HttpPost]
public HttpResponseMessage SaveAccount(HttpRequestMessage request)
{
}
This will allow you to rule out a problem with the Action selection/ model binding. If it still does not match then I suggest you turn on the trace writer and find out why it is failing to find the controller. http://nuget.org/packages/Microsoft.AspNet.WebApi.Tracing/
If it does match the above signature then take a look at this blog post http://www.bizcoder.com/?p=259 for further suggestions on how to debug serialization issues.

Wasn't able to fix this issue. Almost certainly down to some obscure thing not installed or switched on on the server. Very frustrating as other Web Apis are already working on the same box.
Couldn't spend any more time on it, so just reimplemented using Service Stack instead, which just works.

Related

.Cshtml is greyed out

Iam working in MVC 4.0 in VS2010 and the .cshtm looks greyed out and also not showing the intellisense. Even the solution is building without any errors. Any sugessions?
My Root Web Config File shown below:-
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-GatewaySupportTool-20150810125527;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers></system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>
Such this problems occurs when you have an auto updated Visual Studio or a migrated project to new version of platform or IDE. Specially, This is true when you mentioned it's not showing the intellisense and we have parser errors based on some changes in IDE or project configuration to work with each other perfectly.
When you run VS 2010 the first time on a machine that has had VS 2008 installed on it, it asks you if you want to import your existing VS 2008 profile settings. By default this checkbox is checked – which means you by default automatically import your existing settings.
Solutions
Try to work with the same IDE as you created this project
Try to re-install IDE
Take a look at This to see how to correct unwanted changes
Use the Tools->Options menu command, select the Text Editor->C#
settings, and then check the two circled check boxes above
(Auto-list members and Parameter information). Intellisense will
then be turned on and work fine.
I wish to solve the problem.
Finally i solved it by installing Microsoft Visual Studio 2010 Service Pack 1 (Installer).
Please navigate to the link http://www.microsoft.com/en-in/download/details.aspx?id=23691 to download and install. Once installed, Open the Visual studio and look the problem got solved...:)

Npgsql tries to connect to database "postgres"

I have simple ASP.NET MVC 4 application (just created it), that connects to remote PostgreSQL server via NpSQL + EF client. But it tries to connect to database "postgres" which is not allowed:
Npgsql.NpgsqlException: FATAL: 28000: no pg_hba.conf entry for host "myipaddress", user "myuser", database "postgres", SSL on
Other webapp works with the same Web.config without any problems. Only difference I see are different versions of Npgsql and Npgsql.EntityFramework libraries. Working has 2.1.3, faulty one 2.2.1. But it doesn't work, when I downgrade them.
It works on my development machine which has no ph_hba.conf modifications (localhost has access to any user and any database).
Web server is running on Windows Server 2012 R2 + IIS8.5
Why is one app trying to access database "postgres" and other not? How to fix it?
Thanks for any help.
Web.config:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=152368
-->
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=myserver;port=1234;Database=mydatabase;User Id=myuser;Password=xxx;SearchPath=dbo;SSL=True" providerName="Npgsql" />
<add name="DefaultConnection_DatabasePublish" connectionString="Server=myserver;port=1234;Database=mydatabase;User Id=myuser;Password=xxx;SearchPath=dbo;SSL=True" providerName="Npgsql" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<httpRuntime targetFramework="4.5" />
<compilation debug="true" targetFramework="4.5" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
<customErrors mode="Off" />
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.data>
<DbProviderFactories>
<add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<contexts>
<context type="DBConnectionTester.Models.UsersContext, DBConnectionTester">
<!--<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[MvcSlap.Models.UsersContext, MvcSlap], [MvcSlap.Migrations.Configuration, MvcSlap]], EntityFramework, PublicKeyToken=b77a5c561934e089">
<parameters>
<parameter value="DefaultConnection_DatabasePublish" />
</parameters>
</databaseInitializer>-->
</context>
</contexts>
<providers>
<!--<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />-->
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
</providers>
</entityFramework>
</configuration>
It was problem with migrations, which don't work with NPgSQL 2.1. I had them disabled in other webapp, but for some reason, which I don't know, they were active in this app and causing problems. Only difference between local and server database was table __MigrationHistory present on server, which I have renamed to some other name and it works now.
Apologies but I am not allowed to comment.
My problem is at least very similar to OP but I dont understand the solution. I am getting the same NpgsqlException where the database name is "postgres" but should be what I specified in the connectionstring.
Setup
My web server setup is the same, i.e. Windows Server 2012 and IIS
8.5.
The Postgresql database is on its own machine.
Developer machine and web server are currently using the same
database which is defined in 2)
Developer machine connects correctly
Web server gets the NpgsqlException in the OP
Using msbuild.exe to create a package and msdeploy.exe to deploy the package to the web server
Edit
Turns out, allowing access to database "postgres" in the pg_hba.conf file solved the problem for me.

ASP.Net MVC 4 Areas not registering. HTTP 404 error from Visual Studio 2012

I have spent the whole day trying to get an existing MVC application to work.
I have a the following situation:
1) I started working on an existing ASP.Net MVC Application with a custom area
2) It is working well at the office
3) I am at home and I VPN'ed and got the latest code from TFS
4) It is building successfully but if I try to run it from within Visual Studio 2012, it cannot see the registered areas
HTTP 404. The resource you are looking for (or one of its
dependencies) could have been removed, had its name changed, or is
temporarily unavailable. Please review the following
URL and make sure that it is spelled correctly.
A default document is not configured for the requested URL, and
directory browsing is not enabled on the server.
5) I created an ordinary MVC website (NO AreaRegistrations) and ran it in Visual Studio 2012, it gives no errors
6) I have tried RouteDebugger and I keep getting the following error:
Sequence contains no elements
I have also gotten stuck trying to resolve that. If there is another way to actually step into it to see where it is failing, it would help.
In the actual Area folder: MyProject\Areas\MyCustomArea I have
MyProject\Areas\MyCustomArea\AreaRegistration.cs:
using Mvc = System.Web.Mvc;
namespace MyProject.Areas.MyCustom
{
public class AreaRegistration : Mvc.AreaRegistration
{
public override string AreaName
{
get { return "MyCustomArea"; }
}
public override void RegisterArea(Mvc.AreaRegistrationContext context)
{
RouteConfig.RegisterRoutes(context);
}
}
}
And
MyProject\Areas\MyCustomArea\RouteConfig.cs:
using System.Web.Mvc;
using System.Web.Optimization; // <-- DOES THIS REFERENCE HAVE AN IMPACT?
namespace MyProject.Areas.MyCustomArea
{
public class RouteConfig
{
public static void RegisterRoutes(AreaRegistrationContext context)
{
context.MapRoute(
"MyCustomArea_Default",
"MyCustomArea/{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyProject.Areas.MyCustomArea.Controllers" });
}
}
}
My App_Start Folder has NO Route.config.cs file
I have a MyProject\packages.config with the following:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="RazorGenerator.Mvc" version="2.2.2" targetFramework="net45" />
<package id="WebActivatorEx" version="2.0.5" targetFramework="net45" />
</packages>
I use System.Web.Optimization
My Web.config file has the following:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
<add namespace="Kendo.Mvc.UI" />
</namespaces>
</pages>
</system.web>
Is there anything I may need to do to get it to work, bearing in mind that it works at work and I use RazorGenerator as well as Web Optimization, if those have an impact
Sorry if it was too long
Ensure that AreaRegistration.RegisterAllAreas() is called in your application startup. The usual place is in Application_Start in global.asax.

issue deploying asp.net mvc 5 to iis server

I am trying to publish / deploy my site to a iis 7 server.
When I click run in visual studio it works perfectly.
I followed a tutorial to use the web deploy to my server. the files are there and everything seems ok.
but when I go to the site: http://10.0.0.12:8000/
I get: HTTP Error 403.14 - Forbidden, The Web server is configured to not list the contents of this directory.
I have given the site administrator credentials. And I checked there are default pages configured.
What am I doing wrong?
Here is my config File:
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthenticationModule" />
</modules>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Things to try and check:
Set the runAllManagedModulesForAllRequests="true"attribute on the <modules> section in your web.config:
<modules runAllManagedModulesForAllRequests="true">
The website is configured to use an Application Pool using ASP.NET 4.0, Integrated Pipeline mode
ASP.NET 4.0 is registered in IIS. Use the following command to ensure this is the case (might need to adjust the Framework64 folder with Framework if you are on a 32 bit server):
c:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis -ir
Go to the web.config at the wed sites physical deployed locationand open it
And you will find a an xml tag, almost at the end of the xml file, as the following content
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
....
</handlers>
<!--ENTER THE CODE HERE-->
</system.webServer>
At the given location add the following code piece save and refresh ur site
<modules runAllManagedModulesForAllRequests="true">
Also Try removing any othere tags already available!

MVC4 deployed to remote IIS 7.5, gives 401.2 error when accessing sql server

Development of an MVC site to handle tracking data. Target is to use MVC4 and deploy to a Windows 2008 R2 Server running SQL Server 2008 and IIS 7.5. This is run in a domain.
Original setup, which worked, was a development machine using Visual Studio 2012.2 running Windows 8, IIS 8 (not express), and talking to the remote SQL server. Development did not occur using Developer Server. I was able to connect to the the website, run anything, etc. without apparent issue in any logs.
Problem has happened since deployment to the 2008 server and onto IIS 7.5. Using DefaultAppPool mapped to ApplicationPoolIdentity, I created a Web Site with Binding *:80 and attached the deployment. Settings are:
Application pool: DefaultAppPool
.Net Framework Version: 4.0
Pipeline Mode: Integrated
Pass-through authentication.
Test Settings shows message "The application pool identity is valid."
I add a Virtual Directory to the Web Site and Test Settings shows same message.
I convert the Directory to an Application and Test Settings now shows error message "Invalid application path". If I use my own credentials then Test Settings it shows valid.
From IIS on remote server Web Site->Application->Actions->Browse *:80
I receive my home page automatically. Browsing to a page the includes a call to the sql server gives me an error 401.2, which traces to a 404 on a remote machine.
I have Windows Authentication installed on the IIS server, I created login in SQL for IIS AppPool\DefaultAppPool. I added the same to access as a user db_reader and db_writer in the database. I added the identity to read/write and execute permissions for the virtual directory for the website.
I have read everything I can find in my searches on this and everything says the problem is Windows Authentication not being installed or needing to run aspnet_regiis -i... but I did all of it! I am certain this is a configuration issue but I just can't figure it out and have exhausted myself trying to fix this for the last two days.
Web.config sections as follows:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
<httpRuntime targetFramework="4.5" />
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
<identity impersonate="true" />
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="TestAsyncHttpHandler" />
<add name="TestAsyncHttpHandler" path="*.svc" verb="*" type="System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /></handlers>
<modules>
<remove name="WebDAVModule" />
</modules>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.4.0.0" newVersion="5.4.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.4.0.0" newVersion="5.4.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.4.0.0" newVersion="5.4.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
<connectionStrings>
<add name="PerfTrendDataEntities" connectionString="metadata=res://*/Models.PerfTrendDBModel.csdl|res://*/Models.PerfTrendDBModel.ssdl|res://*/Models.PerfTrendDBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=perftrenddb;initial catalog=PerfTrendData;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="TrendRestServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="PerformanceTrendDB.Web.SubmitResults.TrendRestService" behaviorConfiguration="TrendRestServiceBehavior">
<endpoint name="BasicHttp" address="basic" binding="basicHttpBinding" bindingConfiguration="FileUploadConfig" contract="PerformanceTrendDB.Web.SubmitResults.ITrendRestService" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="FileUploadConfig" transferMode="StreamedRequest" />
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
All of this configuration works on the IIS 8 on my development box, I am not sure if there should have been much of a difference with moving to IIS 7.5, I am fairly certain it has to be an issue with communicating to the SQL server from the new machine.
EDIT: Added Log File and Trace File
Flow of operation, open website and click the ManageUsers link. Manage users loads then the api call fails. Looks like a lot of errors are happening from browser attempts to use anonymous initially, then logs in with credentials... so two errors in configuration to fix it looks like.
Log Files Shows:
2013-10-16 03:05:37 10.23.34.146 GET /PerformanceData - 80 - 10.24.158.125 Mozilla/5.0+(Windows+NT+6.2;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/30.0.1599.69+Safari/537.36 401 2 5 609
2013-10-16 03:05:39 10.23.34.146 GET /PerformanceData - 80 AMR\gjmason 10.24.158.125 Mozilla/5.0+(Windows+NT+6.2;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/30.0.1599.69+Safari/537.36 200 0 0 2281
2013-10-16 03:07:01 10.23.34.146 GET /PerformanceData/Home/ManageUsers - 80 - 10.24.158.125 Mozilla/5.0+(Windows+NT+6.2;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/30.0.1599.69+Safari/537.36 401 2 5 15
2013-10-16 03:07:01 10.23.34.146 GET /PerformanceData/Home/ManageUsers - 80 AMR\gjmason 10.24.158.125 Mozilla/5.0+(Windows+NT+6.2;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/30.0.1599.69+Safari/537.36 200 0 0 484
2013-10-16 03:07:03 10.23.34.146 GET /perftrenddb/api/UserManagement - 80 - 10.24.158.125 Mozilla/5.0+(Windows+NT+6.2;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/30.0.1599.69+Safari/537.36 401 2 5 1015
2013-10-16 03:07:03 10.23.34.146 GET /perftrenddb/api/UserManagement - 80 AMR\gjmason 10.24.158.125 Mozilla/5.0+(Windows+NT+6.2;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/30.0.1599.69+Safari/537.36 404 0 2 296
Trace Files shown in condensed snippets:
Summary (1st error)
Url: http://perftrenddb:80/PerformanceData/Home/ManageUsers
App Pool: DefaultAppPool
Authentication: NOT_AVAILABLE
Summary (2nd error - same details)
Url: http://perftrenddb:80/perftrenddb/api/UserManagement
App Pool: ASP.NET v4.0 Classic
Authentication: NOT_AVAILABLE
Summary (3rd error - from a STATUS_CODE 404)
Url: http://perftrenddb:80/perftrenddb/api/UserManagement
App Pool: ASP.NET v4.0 Classic
Authentication: Negotiate
So three things?
Attempts to log in as anonymous user, or is that the Impersonation attempt? Normal?
(Fixed, see Solution 2) The api calls are going to http://perftrenddb:80/perftrenddb/api/UserManagement, not the way I set it up and not the way it ran on my local machine.
The App Pool changed to Classic rather than DefaultAppPool which used Integrated Pipeline, is that a problem?
I can answer the second and third error traces, since that one is in the JS file. It was my fault when I had made it to work in the development environment (this is my first foray into web development, I normally stick to application libraries!). I do have a solution for that part... it has gotten my web site working, although I am still getting the error/traces on the first issue and the App Pool changeover that I don't understand.
I hate weird issues I don't understand so any insight into fixing the 401.2 anonymous login issues would be great!
Solution 2
To fix the context prefix for us noobs out there that hard coded our virtual directory names there is a fix that eliminates the Developer Server/IIS Express/IIS deployment issues.
Add this to your _Layout.cshtml or master page:
<script type="text/javascript">
var config = {
contextPath: '#Url.Content("~")'
}
</script>
Then in your JS file add:
var apiUri = config.contextPath + "api/MyWebApi";
Where MyWebApi is the name of the API Controller you are accessing. And you can now call your getJSON with no problems, i.e.
$.getJSON(apiUri, null).success( /* Your code */ );
As far as i have understood, i can see in the microsoft sites that 401.2 error is because
Authentication was not attempted because the server and client could
not agree on an authentication protocol
Also i can guess that there might be problem with permissions also.
you can use the following tools provided by microsoft to diagnose the issue
Authentication and Access Control Diagnostics 1.0
IIS Diagnostics Toolkit
Hope this might be helpful to some one who are facing the same issue.

Resources