Any reason this WebAPI call works in dev but will not work on Win2012? "Unable to locate a controller for ..." error - asp.net-web-api

I am stuck trying to find the reason this happens. I have a WebAPI service in my module. A specific call to a public entry point (the default path) works fine on my dev machine (Win7, IIS 7.5) but doesn't on the production server (Win2012, IIS8). The DNN installations are clones. Here is the call:
/DesktopModules/DNNEurope/LocalizationEditor/API?tabid=1&moduleid=4
The Win2012 installation replies with "Unable to locate a controller for ..." and then naming the path and namespace this controller is in. Note the routing should work just fine as it works fine in dev.
There is one quirk to note, here. I'm supplying the tabid and moduleid through the querystring, rather than through headers as is the practice when you're doing json exchanges. That is because this call is supposed to be consumed elsewhere. Again, keep in mind this works fine locally.
The route definition is:
mapRouteManager.MapHttpRoute("DNNEurope/LocalizationEditor", "Default", "", New With {.Controller = "Localization", .Action = "ListObjects"}, New String() {"DNNEurope.Modules.LocalizationEditor.Services"})
and the method:
<HttpGet()>
<AllowAnonymous()>
Public Function ListObjects() As HttpResponseMessage
Any ideas?

Kiran's remark put me on the right track. In the project there was a reference to the Mvc library that was not on the server. Strangely enough for the asp.net application this is not a problem but for WebAPI this is. Even though any of the methods in the Mvc library were no longer used as I switched to WebAPI at one point during the project. So locally this Mvc library was still found, but not in production.

Related

Tutorial ASP.NET Core app not seen by localhost

Using MS Visual Studio 2022 on Windows 10 pro to build an ASP.NET Core MVC for the first time. When this tutorial (https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-6.0&tabs=visual-studio) has me run it, Localhost comes up with some stuff relating to the app (its name, a welcome message). The tutorial says to then add "/MvcMovie/" to the URL in the address field. Then, localhost says "No webpage was found for the web address: https://localhost:44379/MvcMovie/".
I'm new at this. Not sure from the tutorial when/if to actually add something to localhost.
Any help will be appreciated.
If you are refering to this:
In the Configure your new project dialog, enter MvcMovie for Project
name. It's important to name the project MvcMovie. Capitalization
needs to match each namespace when code is copied.
The tutorial is saying that your Project's name should be MvcMovie when you create it. Other than that, the tutorial doesn't ask you to go to localhost/mvcMovie. To figure out the routing in an MVC check the next few parts about Controllers-Views-Models. There will be explained about the routing part and how you can access different parts of the URL.
The MvcMovie mentioned in the document does not require you to enter in the URl, it is just the name of the created project:
If you need to use MvcMovie as the routing address in the Url, you can do this:
Controller:
[ApiController]
public class TestController : Controller
{
[HttpGet]
[Route("MvcMovie")]
public IActionResult MvcMovie()
{
return View();
}
}
View demo:
<h1>This is MvcMovie Controller!</h1>
Test url:https://localhost:7040/MvcMovie
Result:
I think you need to know the routing rules in mvc, you can read this document:
Routing to controller actions in ASP.NET Core

How to handle Custom Errors With .Net Core 2.2 MVC

I saw on youtube how to handle custom errors but it was with web.config and in dotnet core 2.2 it does not have this file or I'm not finding it through visual studio 2019.
ASP.NET Core doesn't use Web.config, unless you're hosting in IIS, and then only for minimal IIS module configuration. Custom error handling is done via middleware configuration in your Startup.Configure method. This is actually covered in the default project template, though, so it's odd that you don't have something included by default to at least work from. Regardless, you're looking at something like:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
}
That will generally route any global uncaught exception to a general /Error endpoint, which could be either a controller action or Razor Page. More likely than not, you'll want a little more flexibility, and you will also want to not expose an actual "error" URL in the browser, so you'll probably sub UseExceptionHandler with UseStatusCodePagesWithReExecute:
app.UseStatusCodePagesWithReExecute("/StatusCode","?code={0}");
That will keep the URL, without redirecting and load up a /StatusCode endpoint while passing the specific status code (404, 400, 500, etc.), allowing you to return custom messaging per error type.
All of this and more is in the documentation.

MVC RedirectToRoute in Global asax not working on hybrid site

I recently rebuilt our webforms site as a hybrid mvc3/webforms website, with the newer code being replace with mvc. In the past, there was some code in the Application_Error method of the global asax file redirecting users to a 404
LifespeakExceptions.Log404();
HttpContext.Current.Server.Transfer("~/404.aspx");
However, when I tried to replace this with a route, it couldn't seem to find it - for example I tried this in the registermvc3routes file
routes.MapRoute(
"404", // Route name
"pagenotfound",
new { controller = "Utilities", action = "Error404Page" }
);
and I added this in global asax
LifespeakExceptions.Log404();
Response.RedirectToRoute("404");
However, it can't seem to find this route, even though I know it exists -- calling it directly correctly works. Could this be because it's a hybrid site?

MVC3 SSL Trouble - Can't switch from HTTPS to HTTP when SSL is not required

I'm trying to get my MVC3 site to redirect from HTTPS back to HTTP when the user browses to a page where it's not required (and they aren't logged in). I Don't want to have the load of running the whole site HTTPS but it's looking like thats the way I'll have to go.
I've been having loads of trouble with remote debug and symbols, but having gone back in time to 1985 and using message box equivalents to debug with I've come to the following conclusion:
if (filterContext.ActionDescriptor
.GetCustomAttributes(typeof(RequireHttpsAttribute), true)
.Any()
)
{
return true;
}
return false;
Always returns false.
The controller def starts as:
[FilterIP(
ConfigurationKeyAllowedSingleIPs = "AllowedAdminSingleIPs",
ConfigurationKeyAllowedMaskedIPs = "AllowedAdminMaskedIPs",
ConfigurationKeyDeniedSingleIPs = "DeniedAdminSingleIPs",
ConfigurationKeyDeniedMaskedIPs = "DeniedAdminMaskedIPs"
)]
[RequireHttps]
public class AccountController : Controller
{
And it doesn't seem to work for any actions in this controller (although they do get successfully routed to SSL).
Any suggestions? I'd love to see an answer for what I perceive as my own nubery ;)
Custom NotRequreHttpsAttribute tutorial
I use the above link post to implement my custom attribute, and redirect from https to http. Hope this helps.
My problem was discovered to be related to the bindings on the server published to. We use the same server for stage and production, and the stage https bindings were not set, so whenever it was calling an https page it was resolving into our production site (which looked the same so it was hard to spot).
Once I added a binding it was all solved. My code was ok...

Introducing MVC into ASP.Net - 404 error

I have an existing ASP.Net Application, into which I am attempting to introduce MVC2.
Hopefully I can remember the steps, but what I did was the following:
Created a dummy MVC2 project.
Compared and merged .csproj, resulting in the Add Item commands showing MVC2 items.
Compared and merged the web.config
Compared and merged the global.asax.cs
Added Models, Views and Controllers directories
Added HostController with Index action and Index.aspx (no logic)
Amended route to make /Host/Index the default
So now, when I access the application via the root address http://localhost/MyApp it all works.
But, when I access http://localhost/MyApp/Host/Index I get a 404 error. I get the same result for any of the Controller/Actions I created. The only way I can get them to appear is to use the defaults in the routing configuration. I installed Phill Haack's route debugger and it's doing nothing. Obviously there's some problem with my routing, but I can't figure it out.
Any ideas what I've missed?
Bah... turns out that this is to do with IIS 5.1 and MVC routing.
I resolved it by using the following Routes in my application (note the .aspx extensions).
routes.MapRoute("Root", "", new { controller = "Host", action = "Index" });
routes.MapRoute("Default", "{controller}/{action}.aspx", new { controller = "Host", action = "Index" });
Means I can't have clean routes, but at least it works.

Resources