DNN Web API Server Error - c#

I'm repeatedly getting an Internal Server Error 500 when trying to make a simple call to a DNN Web API controller. I do not get this error on my development machine, nor do I get it on one of our deployment servers. However, one of our other deployment servers is the problem; and I'd like to figure out why.
Consider the following, simplistic API controller in TheBestController.cs
namespace MyTestLibrary.Controllers
{
public class TheBestController : DnnApiController
{
[DnnAuthorize]
[HttpPost]
public HttpResponseMessage TestMe()
{
return Request.CreateResponse(HttpStatusCode.OK, "I'm working");
}
}
}
With this RouteMapper in RouteMapper.cs
using DotNetNuke.Web.Api;
namespace MyTestLibrary
{
public class RouteMapper : IServiceRouteMapper
{
public void RegisterRoutes(IMapRoute mapRouteManager)
{
mapRouteManager.MapHttpRoute("TheBestController", "default", "{controller}/{action}", new[] { "MyTestLibrary.Controllers" });
}
}
}
With the following AJAX call from my Javascript:
$.post("DesktopModules/MyTestLibrary/API/TheBest/TestMe", function(data) {
alert(data);
});
I have made sure that all of the correct permissions are set for the DNN application in IIS (as well as mirrored those and all other pertinent settings that are on our server that has this working). I place the compiled MyTestLibrary.dll in the bin folder of the DNN site, and I place the javascript file in the Resources/Shared/scripts folder of the DNN site. The DNN site is reading the javascript with no issues, and I am not using a module (and would not like to because I want all of my code to be easily transferrable to an MVC site).
I have also tried using the [AllowAnonymous] tag on the controller method (to no avail).
The version of DNN on all machines is 07.01.01, and the two servers are running Server 2008 R2 64bit. The dev machine is running Win 7 64 bit.
Any ideas?

To break the comments out into an answer ;)
I've seen where IIS on Windows Server 2008 can somehow corrupt a folder that a DNN website is using, and that starts to cause issues with certain web services in DNN. Two solutions that I know have worked in the past for me.
1) Setup a new site in IIS and install DNN in that folder.
or
2) Move the DNN files to a different folder on the server, point the existing IIS website to that folder, instead of the original one.
One of those two should hopefully work for most people that run into this problem.
Now, what exactly goes wrong in IIS and why can the existing folder be fix? No clue :(

Related

Kentico v8.2.12: Why is my WebAPI Route not registered correctly (usually only on the first release)?

Summary
A Kentico 8.2 website fo which I have recently implemented a Web API service isn't registering routes on first deployment and all calls return 404. Further redeployments usually fix the issue, but I would like to fix it permanently before it is released to PROD.
What is preventing the first deployment from registering the route properly?
Background
We have a Kentico v8.2.12 website that uses Web Forms using .NET Framework v4. I have registered a Web API Controller, but it appears on the first release the route isn't registered and any calls to the service returns "404 (Not Found)".
When I first deployed to the DEV environment the Web API route wasn't registered, but upon deploying another build it magically worked. One or two other releases into the DEV environment caused similar issues, but in these instances re-deploying the same build worked.
The same issue has now occurred when released to UAT, however as the deployments are carried out by another team it will be more time-consuming to re-deploy builds and looks unprofessional. I am also wary of this occurring in PROD---which may cause the live website to be down further than necessary.
Web API Implementation
The Web API Controller is inside the CMS Website project and not a separate library.
Global.asax.cs
The Global.asax.cs file's Application_Start() method registers the route and looks similar to the below:
protected void Application_Start()
{
// Scripts Bundling here, which havs been removed for brevity
BundleTable.EnableOptimizations = false;
// Registering the API
RouteTable.Routes.MapHttpRoute(
name: "DefaultApiWithAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new {id = RouteParameter.Optional}
);
}
MyController.cs
My Controller looks similar to the below stored under the CMS website: CMSApp/ApiControllers/MyController.cs
[assembly: RegisterApiController(typeof(CMSApp.ApiControllers.MyController))]
namespace CMSApp.ApiControllers
{
public class MyController : ApiController
{
Channel Channel = new Channel();
[HttpPost]
public int Create()
{
Response objResponse = Channel.Instance.DoSomething();
HandleResponse(objResponse);
return objResponse.SessionHandle;
}
}
}
In the webbrowser, accessing /api/my/create returns a 404 (Not Found), but I expect it to tell me it's a POST method.
Lib versions [Edit, I have since updated the libs but issue still prevails]
Microsoft.AspNet.WebApi : v4.0.30506
Microsoft.AspNet.WebApi.Client : v4.0.30506
Microsoft.AspNet.WebApi.Core : v4.0.30506
Microsoft.AspNet.WebApi.WebHost : v4.0.30506
Question?
Why do the first deployments into an environment not work, but most further deployments work as I expect them to?
The issue was due to ASP.NET caching.
Once "MS-ApiControllerTypeCache.xml" was removed under "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files" and IIS was restarted, the controller was picked up.

Cannot reach an otherwise reachable web API, with dots in URL

I have a web service deployed on IIS 8.5 (Windows Server 2012 R2) which has to be called passing four arguments. They are not optional, and are to be included directly in the URL, without a query string, as such:
.. api/myservice/argument1/argument2/argument3/argument4
If I try to call it with the URL written above, it answers and gives the expected response. However, a 404 - Not Found error is given instead when trying to use real production-like arguments as such:
api/myservice/AAAA_AAAAA.AAA_AAA_AA_AA_AAA_000000_000000_000000/333/AAA/AAA.AAA.AA.AA.AAA.000000.000000.000000
I thought the multiple dots raised the issue, so I replaced each one of them with %2E, but nothing changed.
I've searched for already answered questions: I tried this and this, to no avail.
What is the problem with that production-like URL?
Here is the code of the API:
[RoutePrefix("api/myservice")]
public class MyServiceController : ApiController
{
[HttpGet]
[Route("{argument1}/{argument2}/{argument3}/{argument4}")]
public string StartValidation(string argument1, string argument2, string argument3, string argument4)
{
// operations...
}
}
I traced the request with IIS Failed Request Tracing as suggested in an answer, but I'm not able to find a clue in the resulting log:
Can anyone help me?
Enable failed request tracing on IIS to check.
https://learn.microsoft.com/en-us/iis/configuration/system.applicationhost/sites/site/tracefailedrequestslogging
Good possibility that IIS is rejecting these as part of URLScan (if enabled), but FRT will confirm.
First try to deploy it to your local Iis, if it works, means that your production can have a different configuration at the level of the IIS, or the compiled code is not the same as the one you have and you need to redeploy the api.
If the api fails at the level of your local IIS and it runs properly from the vs(if you pass any parameter with the postman) you hit a backend breakpoint, then you have a wrong configuration on your deployment on your local IIS.
If you never hit the breakpoint you have an error at the level of your code.
Hope this helps
I finally solved this. I added a piece code, provided by this answer to a quite identical question, to the web.config, but a different issue arose: it seemed IIS had to be run in "Integrated Mode". Thanks to the clue given by this answer to a question about this subsequent problem, I switched the Managed pipeline mode of the application pool where the API was deployed from Classic to Integrated, which allowed that code to work. The web API finally accepts arguments with periods.

API Calls to controller is breaking after deploying to IIS

I am invoking API calls to get JSON data from server using javascript.
$(function () {
var customersTable = $('#Customers');
var returnCustomersTable = UpdateDataTable(customersTable, "/Customers/Loaddata");
}
This works fine when I am working with Visual studio Dev environment , because my web application is on root and so all javascripts Works fine.
Example:
My WebSite URL: http://localhost:4391
API Calls will be: http://localhost:4391/Customers/Loaddata
This works fine.
But when I deploy application to IIS, my website URL will be,
My WebSite URL: http://localhost/MyAppName
But API calls would still be,
API Calls will be: http://localhost/Customers/Loaddata, which results in not found.
Since I am using javascript in a separate file , I wont be able to do URL.Action.
The other option would be to create a base URL for dev and prod and the append with each servcie call.
But I am thinking if there is any other way.
The routing configuration in ASP.NET MVC is designed to serve as the single place in the application where all of the URLs can be maintained.
It helps considerably with maintenance of the project if all URLs are generated using routing through one of the UrlHelper based methods, such as Url.Action() rather than hard coded in controllers and views.
As long as your JavaScript is in an MVC view, you can just resolve Url.Action inline.
$(function () {
var customersTable = $('#Customers');
var returnCustomersTable = UpdateDataTable(customersTable,
'#Url.Action("Loaddata", "Customers")');
}
If the JavaScript is in a separate file outside of MVC, consider passing the URL through a variable or function parameter.
This routing feature also makes it easier to deploy ASP.NET MVC, because the generated URLs will adapt to use the application path when the application is not deployed to the root website virtual directory.
Application running in IIS web site root:
/Customers/Loaddata
Application running in virtual subdirectory configured as IIS Application named "MyAppName":
/MyAppName/Customers/Loaddata

Hosting an MVC6 application under default website

I have hosted many ASP.NET and MVC applications before. I was playing with MVC6 lately and tried to host an MVC6 application.
Everything works fine if I host it as a new website. When I host it as an application under the default website of IIS, it only shows an empty page. Please find the log information below
warn:
Microsoft.Extensions.DependencyInjection.DataProtectionServices[0]
Neither user profile nor HKLM registry available. Using an ephemeral key repository. Protected data will be unavailable when
application exits. warn:
Microsoft.AspNet.DataProtection.Repositories.EphemeralXmlRepository[0]
Using an in-memory repository. Keys will not be persisted to storage. Hosting environment: Production Now listening on:
http://localhost:23000 Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNet.Hosting.Internal.HostingEngine[1]
Request starting HTTP/1.1 GET http://localhost/MVC6 info: Microsoft.AspNet.Hosting.Internal.HostingEngine[2]
Request finished in 0.0687ms 404
Note: Default website uses application pool asp.net4.5 and my MVC6 application uses it's own application pool as mentioned in http://docs.asp.net/en/latest/publishing/iis.html#iis-server-configuration
Anyone else faced similar problem like this? I want to know how to make it work under default website
Edit: This is a bug in vs2015 rc1 https://github.com/aspnet/Hosting/issues/416#issuecomment-149046552.
There is a workaround for this bug in above link but I followed a different method because I need to host my application in more than one website without republishing.
I resolved the above problem using the Route attribute. After adding the route attribute to an action result, application works fine
public class HomeController : Controller
{
[Route("MVC6")]
[Route("")]
public IActionResult Index()
{
return View();
}
}

Problems with Mono, MVC, and FastCGI

I have two websites that I have created with Mono.NET. Both were built exclusively using Xamarin Studio (e.g. MonoDevelop). The first is a standard Asp.NET webforms application and the second is an Asp.NET MVC application.
I have deployed the first application to my RedHat 7 server successfully. The server is running NGINX as the pass through and leverages FastCGI. The web application itself is run at the root (/var/www/html/) and thus pointing to http://[myserver].com/ kicks of the application. This all works beautiful.
My second application (the MVC one) should run as a sort of virtual directory of the root. So pointing to http://[myserver].com/admin/ should access the MVC application. This has been configured correctly on the server and when you point to http://[myserver].com/admin, it clearly attempts to start the MVC application.
My problem is that trying to access http://[myserver].com/admin, throws an application exception:
System.Web.Compilation.ParseException
Cannot find type Letters.Web.Admin.MvcApplication
Description: Error parsing a resource required to service this request. Review your source file and modify it to fix this error.
Details: Cannot find type Letters.Web.Admin.MvcApplication
Error origin: Parser
Error source file: /var/www/html/admin/Global.asax
Error source context:
Error lines: 1, 1
1: <%# Application CodeBehind="Global.asax.cs" Inherits="Letters.Web.Admin.MvcApplication" Language="C#" %>
I've been googling about this error for 2 days with no luck. I have double checked to make sure the /bin directory exists in the /admin folder (it does) and that all of the appropriate .dll files are present including the web application dll (also present). I have made sure my Global.asax has the appropriate declaration (it does ... see below)
<%# Application CodeBehind="Global.asax.cs" Inherits="Letters.Web.Admin.MvcApplication" Language="C#" %>
I have also made sure the namespace is correct in the codebehind (it is... see below)
namespace Letters.Web.Admin
{
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes (RouteCollection routes)
{
routes.IgnoreRoute ("{resource}.axd/{*pathInfo}");
routes.MapRoute (
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}
public static void RegisterGlobalFilters (GlobalFilterCollection filters)
{
filters.Add (new HandleErrorAttribute ());
}
protected void Application_Start ()
{
AreaRegistration.RegisterAllAreas ();
RegisterGlobalFilters (GlobalFilters.Filters);
RegisterRoutes (RouteTable.Routes);
}
}
}
I cannot for the life of me figure out the issue. Everything appears to be functioning correctly. This application runs fine on my mac using Xamarin studio. I've double checked and I have the same version of mono installed on my mac as I do on the server. I'm guessing there is probably something else going on that is being obfuscated by this error but for the life of me, I can't figure out the issue. Does anyone have any ideas?

Categories