I have a slightly different requirement. I have two projects and both are Asp.Net 4.7.2 Web APIs.
Project A:
This project is using Controllers which is implementing ApiController and using IHttpActionResult and HttpRequestMessage.
public class LoginController : ApiController
Project B:
This project is using Framework 4.7.2 but also using AspNetCore libraries. It is implementing
[ApiController]
public class LoginController : ControllerBase
Project A is using Microsoft.Owin for Google Login and using,
MessageRequest.GetOwinContext().Authentication.Challenge(properties, AuthenticationProvider);
When LoginController from Project A is called in the browser, it redirects the user to Google Login screen which is the desired behavior.
I have imported the LoginController from Project A into the Project B as a new Controller. The project is build fine but when I run the Project B, it does not hit the new LoginController.
I am not sure how to configure the routes for ApiController inside Project B. Project B is using Startup.cs class to configure the routes and Project A is using a class WebApiConfig inside App_Start.
I want the ApiControllers to hit as other Controllers are getting hit in Project B.
Related
I am Trying to call a web service in Xamarin Android Project. But when adding service reference in my project and trying to create reference of that web service it says 'HelloService' is a namespace but is used like a type.
MainActivity.cs
Solution Structure:
This is not an issue with xamarin.android, it's an issue with naming, i assume you have something like this.
namespace HelloService
{
class HelloService
{
}
Don't use same name for classes and namespace, more detail here
I have multiple controllers in my ASP .NET Core application and I am using ReponseCache attribute like this on a few methods:
//controller
[Route("api/[controller]")]
[EnableCors("CorsPolicy")]
public class InsightsApiController : Controller
//method
[Route("CoursesTextContent")]
[HttpGet]
[DecryptFilter]
[ResponseCache(Duration = 60)]
public IActionResult GetCoursesContent(string locale, string tabKey, string widgetType)
The issue that I am having is that for one controller this is working fine and I can see the response in chrome dev tools with "Cache-Control:public max-age=60" but in a different controller when I add this attribute its adding "Cache-Control:no-cache". I compared both controllers and methods in them and they are configured same. I have also tried to add ASP.NET Core middleware recommended here but same results. I am calling both methods from Angular2 webpage. Is there something I can do from the client side (request)? or something in the ASP.NET Core app setup?
You are missing a trailing parenthesis
[ResponseCache(Duration = 60]
needs to be
[ResponseCache(Duration = 60)]
I had a session middleware enabled in the startup.cs file of my ASNETCore webapi project. I removed it and its working for all calls/controllers now. Not sure why it was causing problem only with one Controller.
When pressing F12 (Go To Definition) on Controller in Visual Studio it shows you the abstract base class.
public abstract class Controller : IActionFilter, IFilterMetadata, IAsyncActionFilter, IDisposable
But when looking up Controller.cs on GitHub. It shows that it inherit from ControllerBase.
public abstract class Controller : ControllerBase, IActionFilter, IAsyncActionFilter, IDisposable
Why is this?
I am confused. Also, since HomeController inherit from Controller how can Controller inherit from ControllerBase when C# does not support multiple inheritance?
The two are from different points in time. So for example the file you linked to on GitHub is the current version of the file, note that it's from the Dev branch.
And more than likely you are not running code from the current dev branch.
If you click on the history button on GitHub for the file you linked to you will see the revision history for the file.
I checked the various version of this file by clicking on the <> button for each revision but non match the version of the code you are running. I see that the revision history for this file only goes back to Jan 22 2016. So prior to that the Controller code must have been defined in a different file or for some other reason the revision history was lost (Possibly when they renamed it from MVC 6 to Core MVC 1.0).
More than likely you are running code from RC1. That version of the Controller.cs on GitHub more closely matches what you are seeing: https://github.com/aspnet/Mvc/blob/6.0.0-rc1/src/Microsoft.AspNet.Mvc.ViewFeatures/Controller.cs It should match perfectly if it's the right version of the code but I see that it's still slightly different. It matches in the sense that it does not inherit from ControllerBase.
Also with regard to multiple inheritance. When HomeController inherits from Controller and Controller inherit from ControllerBase that's not multiple inheritance. Multiple inheritance would be if there were a ControllerBase and let's say a SomethingElse class and the Controller inherited from both of these classes in the controller definition. You are correct that C# does not support this.
ASP.NET WebAPI has a much appreciated ability to discover ApiController classes in external DLLs even if those DLLs are not referenced. For example, I may have MyWebApiProject that has a set of ApiControllers. I could then create a completely separate project called MyApiProjectPlugin that contains ApiController classes also. I have been able to add the MyApiProjectPlugin.dll file to the bin folder with the first MyApiProject.dll and the original project will discover all the controllers in the plugin project. I really like that ability.
However, What I would like to do is be able to add the plugin project to a sub directory inside of the bin folder. Something like bin/plugins. When I tried this, the original MyApiProject was unable to discover the plugin's controllers.
Is there a simple way to get WebAPI to look for ApiController classes in the bin's subdirectories? If I can avoid rewriting a controller factory from scratch I would like to.
You can write an assembly resolver.
public class PluginsResolver : DefaultAssembliesResolver
{
public override ICollection<Assembly> GetAssemblies()
{
List<Assembly> assemblies = new List<Assembly>(base.GetAssemblies());
assemblies.Add(Assembly.LoadFrom(#"<Path>\MyApiProjectPlugin.dll"));
return assemblies;
}
}
In the Register method in WebApiConfig, register the resolver.
config.Services.Replace(typeof(IAssembliesResolver), new PluginsResolver());
I'm using AttributeRouting in my Web API project. I've installed the AttributeRouting for Web API. I want to define an Enum route constraint so I setup my AttributeRoutingHttpConfig config as follows:
using System.Reflection;
using System.Web.Http;
using AttributeRouting.Web.Http.Constraints;
using AttributeRouting.Web.Http.WebHost;
using MyProject.Data.Models;
[assembly: WebActivator.PreApplicationStartMethod(typeof(PhantasyTour.AttributeRoutingHttpConfig), "Start")]
namespace MyProject
{
public static class AttributeRoutingHttpConfig
{
public static void RegisterRoutes(HttpRouteCollection routes)
{
routes.MapHttpAttributeRoutes(
config =>
{
config.AddRoutesFromAssembly(Assembly.GetExecutingAssembly());
config.InlineRouteConstraints.Add("ListType", typeof(EnumRouteConstraint<ListType>));
});
}
public static void Start()
{
RegisterRoutes(GlobalConfiguration.Configuration.Routes);
}
}
}
When I fire up my application I immediately receive the following error:
The constraint "AttributeRouting.Web.Http.Constraints.EnumRouteConstraint`1[[MyProject.Data.Models.ListType, MyProject.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" must implement System.Web.Routing.IRouteConstraint
I've looked at the source code for the AttributeRouting.Web.Http.Constraints.EnumRouteConstraint and confirmed that it implements IHttpRouteConstraint which presumably is the WebAPI equivalent of IRouteConstraint in the MVC namespace.
Does anyone know what I'm doing wrong and how I can get this working?
UPDATE:
I attempted to create a completely blank Web Application and add only WebAPI and AttributeRouting for WebAPI references. Despite having absolutely no references to MVC assemblies, I still receive the same error message. I did discover however that there is another EnumRouteConstraint found in the AttributeRouting.Web.Constraints namespace which works perfectly. It doesn't appear to be MVC specific since it is located in the Core AttributeRouting assembly. I would love to know why there are two different EnumRouteConstraint classes when only one of them works. But that is a question for another time.
It is interesting that the exception you get refers to the MVC interface from the namespace System.Web.Routing.
I would take it as a clue and look at all the references in your project, any place in the config where MVC Routes and Http Routes could have been mixed up.
If possible and if you have any at all, try removing all references to MVC (or System.Web.Routing for a start), and MVC flavour of attribute routing (if it's a separate dll).