Calling WebApi Controller action, causes Inheritance security rules violated HostedHttpRouteData.get_Route( - asp.net-mvc-4

I have a .Net Mvc4 project, I've added a WebApi Controller and a basic action in for an ajax call.
When I call the method I get:
Inheritance security rules violated while overriding member:
'System.Web.Http.WebHost.Routing.HostedHttpRouteData.get_Route()'.
Security accessibility of the overriding method must match the
security accessibility of the method being overriden.
I haven't made any custom modifications or set up anything for web Api to work (was I supposed to?)
I noticed the DefaultApi route is in my routeConfig.
I'm also using Unity Mvc4 & Unity Web Api packages, (which I've disabled to test if it was causing the issue but doesn't seem to be).
My controller is TestController
action:
public bool ClearAwaitingNotifications()
{}
& ajax call is going to: api/test/clearawaitingnotifications
How can I fix this?

My exception occured when hitting controller because:
WebApiConfig.Register(GlobalConfiguration.Configuration);
was never called, although the route existed inside my Routeconfig.cs. I think this occurred because I installed my Mvc4 project using a template, which installs some WebApi stuff and it is all a bit old. I then added this line into Global.ascx and received the same security warning but for this line in Global.ascx.
https://stackoverflow.com/a/18713412/314963 then helped me resolve the issue, and now everything is working. In short, it is because of installing Mvc project with a template, or because one of the libraries in webapi 4 was outdated.

Related

login.aspx was not found

I originally created a ASP.NET MVC4 project using the wrong authentication/template in Visual Studio. So I've been trying to make it use windows authentication (I already changed the project property to enable it) but there was a bunch of built-in garbage in my project:
- AccountController
- AuthConfig.RegisterAuth() im my Global.asax.cs
- AccountModel.cs
... and some other crap that I can't remember at this point
I went through and deleted all that junk but I still get the following error:
Error Page 404: The controller for path '/login.aspx' was not found or does not implement IController.
So it mentions some ASPX page in my ASP.NET MVC project that doesn't even exist. It would appear as though I didn't thoroughly go through and remove every reference to the "built-in" authentication login garbage that was put into the project when I first created it.
Can anyone provide any insight?

EnableDependencyInjection in OData extension library

Our service has been using ApiController with EnableQuery attribute and we were using System.web.http.odata (odata < 6). When we moved to new ODataControllers in Odata 6.0 dlls, we started getting error no non-odata route registered.
This got solved by using EnableDependencyInjection which apparently creates non-odataroutes containers.
My question is what could be the other side effect (security, route configuration) of calling this method.

Error management in ASP.NET Core using a BaseController

In previous ASP.NET versions I was used to create a BaseController inherited from the other controllers and there intercepting the general error and logging each error with a simple logging method and passing the ExceptionContext filterContext.
Should I do the same in ASP.NET 5?
I see in file Startup.cs that there is a if/else statement that basically separate the debug/live condition, with line
app.UseErrorHandler("/Home/Error");
for a production application.
How am I supposed to hook in the process and log the errors?
Handling and logging errors in ASP.NET 5 involves a few elements.
To handle the error in a production scenario and show an error page, the app.UseExceptionHandler() method is the way to go. The Diagnostics repository on the ASP.NET GitHub organisation includes a sample that shows this. For development-time scenarios where you want to see a full stack trace and other diagnostic information, use app.UseDeveloperExceptionPage() as seen in this sample.
If the application is using ASP.NET MVC 6, then there is an MVC-specific way of handling errors, much as there was in earlier versions of ASP.NET MVC. In ASP.NET MVC 6, a filter (or the Controller itself, which is also a filter) can handle/override the OnActionExecuted method and inspect the ActionExecutedContext parameter to attempt to handle the error.
When it comes to logging, there's a new logging infrastructure in ASP.NET 5 that reports a great deal of information to any registered ILogger. The default project templates in Visual Studio 2015 register some loggers that log errors (and other data) to the console and the Visual Studio debug output window. However, when running in IIS or IIS Express there is no console window (yet!). But if you run from the command line (using dnx web) then you'll see the error. Or, of course, you can register a different logger that writes to a log file or database and see the logs there.
To register a custom ILogger in a web app:
Write a logger that implements the ILogger interface. See the DNX implementations for how to do this.
Add a parameter of type ILoggerFactory to your app's Startup class's Configure method.
In the Configure method call loggerFactory.AddProvider(<some provider>) and pass in an instance of your logger.

SignalR and Ninject - Ninject not returning a controller for the route

I'm trying to set up SignalR in an existing ASP.Net MVC 4 project that uses Ninject for DI, however whenever I try and access the SignalR default route (which I have confirmed is in the route table), I get the following exception thrown:
Exception message: The IControllerFactory 'Infrastructure.NinjectControllerFactory' did not return a controller for the name 'signalr'.
I have tried injecting the Ninject kernel into SignalR via:
GlobalHost.DependencyResolver = new NinjectSignalRDependencyResolver(ninjectKernel);
And I am setting the default routes via:
RouteTable.Routes.MapHubs();
in Global.asax Application_Start
I can't seem to find any way to resolve this issue.
Ok, resolved it myself - it was not a ninject issue, it was a routing order issue.
I needed the ~/signalr route to be higher up in the routing table order, which meant shifting the RouteTable.Routes.MapHubs(); entry to the top of my Application_Start, above the RegisterRoutes call.

Error when calling MvcHttpHandler.ExecuteRequest from custom IHttpHandler

I have a custom IHttpHandler that calls MvcHttpHandler implemented as described in this answer.
It worked well in asp.net MVC2, but after I migrate the code to MVC4 with IISExpress 7.5, I start getting InvalidOperationException on the line:
httpHandler.ProcessRequest(HttpContext.Current);
with message:
'HttpContext.SetSessionStateBehavior' can only be invoked before
'HttpApplication.AcquireRequestState' event is raised.
ASP.NET Development Server does not make any problems.
Does anyone know what's going on here, and how to solve it?
I believe you need to use httpContext.Server.TransferRequest with the MVC update.
See this question: MVC3 Application Inside Webforms Application Routing is throwing a HttpContext.SetSessionStateBehavior Error in IIS7.5