asp.net-web-api2

How to inject ApplicationUserManager with unity

妖精的绣舞 提交于 2019-11-29 04:14:43
I have ApplicationUserManager defined like this: public class ApplicationUserManager : UserManager<ApplicationUser, int> { public ApplicationUserManager(IUserStore<ApplicationUser, int> store) : base(store) { } public override Task<IdentityResult> CreateAsync(ApplicationUser user, string password) { var result = base.CreateAsync(user, password); //... Repository.DoSomething(...); //Repository is null here //.. } [Dependency] public IRepository Repository { get; set; } } For some reason my repository in not getting injected. Repository is always null I also have this line in my unity

Web API optional parameters

半世苍凉 提交于 2019-11-29 02:01:24
问题 I have a controller with the following signature: [Route("products/filter/{apc=apc}/{xpc=xpc}/{sku=sku}")] public IHttpActionResult Get(string apc, string xpc, int? sku) { ... } I call this method with following URIs: ~/api/products/filter?apc=AA&xpc=BB ~/api/products/filter?sku=7199123 The first URI works without issue. The second one has a strange side effect. Even though the default values for apc and xpc should be null when not provided, the parameters are actually their names. I can

Using Simple Injector in Web API and OWIN

半城伤御伤魂 提交于 2019-11-29 01:39:52
I'm experiencing the same problem as described here and my set up is almost identical to this that is actually based on this guide . When I access a method in my controller I get this An error occurred when trying to create a controller of type 'TestController'. Make sure that the controller has a parameterless public constructor. Here's the stack trace at System.Web.Http.Dispatcher.DefaultHttpControllerActivator .Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor .CreateController

Autofac dependency injection in implementation of OAuthAuthorizationServerProvider

不问归期 提交于 2019-11-29 01:15:10
I am creating a Web Api application and I want to use bearer tokens for the user authentication. I implemented the token logic, following this post and everything seems to work fine. NOTE: I am not using the ASP.NET Identity Provider. Instead I have created a custom User entity and services for it. public class Startup { public void Configuration(IAppBuilder app) { ConfigureOAuth(app); var config = new HttpConfiguration(); var container = DependancyConfig.Register(); var dependencyResolver = new AutofacWebApiDependencyResolver(container); config.DependencyResolver = dependencyResolver; app

Web Api 2 Preflight CORS request for Bearer Token

。_饼干妹妹 提交于 2019-11-29 00:54:15
问题 I have a web-app with an AngularJS front-end and a Web Api 2 back-end, and it uses bearer-tokens for authentication. All is well in FireFox & IE, but with Chrome, my initial login request is SOMETIMES pre-flighted. Here's the call from the AngularJS service: $http.post( http://localhost:55483/token , data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (response) { ... }); The preflight request gets kicked back with an "Allow-Access-Control-Origin"

Web API 2 download file using async Task<IHttpActionResult>

家住魔仙堡 提交于 2019-11-29 00:48:22
问题 I need to write a method like below to return a text document (.txt, pdf, .doc, .docx etc) While there are good examples of posting file in Web API 2.0 on the web , I couldn't find a relevant one for just downloading one. (I know how to do it in HttpResponseMessage.) public async Task<IHttpActionResult> GetFileAsync(int FileId) { //just returning file part (no other logic needed) } Does the above needs to be async at all? I am only looking to return stream. (Is that okay?) More importantly

Ambiguous Controller Names with Routing attributes: controllers with same name and different namespace for versioning

孤者浪人 提交于 2019-11-28 23:22:55
I am trying to add API versioning and my plan is to create a controller for each version in different namespace. My project structure looks like this (note: no separate area for each version) Controllers | |---Version0 | | | |----- ProjectController.cs | |----- HomeController.cs | |---Version1 | |----- ProjectController.cs |----- HomeController.cs I am using RoutingAttribute for the routes. So, ProjectController in Version0 has function with route as namespace MyProject.Controllers.Version0 { class ProjectController : BaseController { ... [Route(api/users/project/getProjects/{projectId})]

Autofac RegisterInstance vs SingleInstance

二次信任 提交于 2019-11-28 22:17:53
问题 IProductRepositoryProxy ProductDataServiceProviderInstance = new ServiceProductDataProvider(); builder.RegisterInstance(ProductDataServiceProviderInstance).As<IProductRepositoryProxy>(); VS builder.RegisterType<ServiceProductDataProvider>().As<IProductRepositoryProxy>().InstancePerRequest(); I saw this code from an ex-employee here and wonder if the guy wanted to register a .SingleInstance() behavior. builder.RegisterType<ServiceProductDataProvider>().As<IProductRepositoryProxy>()

How do I get StructureMap working with an AngularJs / MVC5 and WebApi2 web project

ε祈祈猫儿з 提交于 2019-11-28 21:52:01
So I have an AngularJs/MVC project with normal Controllers and decided to move more to an SPA app and add WebApi2 to pass data back to my UI instead of using MVC. In my Global.asax I had the following for my MVC project: DependencyResolver.SetResolver(new StructureMapDependencyResolver(container)); My WebApiController has a constructor with an IRepository to talk to the database and get some entities back. When my AngularJS web app makes a call to the controller the break points never hit and I'm getting server 500 errors returned with very little information. Public class MyController :

Dependency injection not working with Owin self-hosted Web Api 2 and Autofac

与世无争的帅哥 提交于 2019-11-28 21:12:00
I'm finding my feet with Web Api 2, Owin and Autofac and need some guidance, please. Overview I have an Owin self-hosted Web Api that uses Autofac for IoC and dependency injection. The project is a console app acting like a service, meaning it can be stopped and started. I have an Authentication controller with two constructors: one parameter-less and the other injects a repository. Problem When I run the service and call the api, my parameter-less constructor is called and my repository never gets injected (_repository = null). Research I've done a fair bit of research and found some helpful