简体   繁体   English

OWIN中间件似乎不起作用

[英]OWIN middleware doesn't seem to work

I've recently started learning how to use Owin specification. 我最近开始学习如何使用Owin规范。 I've added Owin packages, which are listed below to Web API 2 project. 我已经添加了Owin软件包,这些软件包在下面的Web API 2项目中列出。 I've commented out the usage of WebApiConfig, and moved routes config and Elmah Logger to Startup class as you can see below - 我已注释掉WebApiConfig的用法,并将路由配置和Elmah Logger移至Startup类,如下所示-

Startup.cs - [assembly: OwinStartup("DataServicesStartup", typeof (Startup))]
public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCors(CorsOptions.AllowAll); 
            var config = new HttpConfiguration();                               
            config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new {id = RouteParameter.Optional});
            config.Services.Add(typeof (IExceptionLogger), new ElmahExceptionLogger());
            ConfigureAuth(app);
            JwtConfig.ConfigureOAuth(app);
            app.UseWebApi(config);
        }

Commented out WebApiConfig usage - 注释掉了WebApiConfig的用法-

 protected void Application_Start()
            {
                WebLocatorIoC.RegisterServices();

                AreaRegistration.RegisterAllAreas();
                //GlobalConfiguration.Configure(WebApiConfig.Register);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }

However, when I'm trying to go to specified routes nothing happens. 但是,当我尝试去指定的路线时,什么也没有发生。 For example when I try to make a request to Controllers/ValuesController I use localhost:1111/api/values. 例如,当我尝试向Controllers / ValuesController发出请求时,我使用localhost:1111 / api / values。 Also Owin.Cors doesn't seem to work too. 另外,Owin.Cors似乎也不起作用。 Only when I use routing and Cors in WebApiConfig it works, in this case WebApi.Cors. 仅当我在WebApiConfig中使用路由和Cors时才起作用,在本例中为WebApi.Cors。

I have the following Owin packages installed: 我安装了以下Owin软件包:

Microsoft.AspNet.Identity.Owin                                                                                                                 
Microsoft.AspNet.WebApi.Owin                                            
Microsoft.Owin                                                                  
Microsoft.Owin.Cors.                                                               
Microsoft.Owin.Diagnostics                                                                                      
Microsoft.Owin.Host.HttpListener                        
Microsoft.Owin.Host.SystemWeb                                                                   
Microsoft.Owin.Hosting                                                                               
Microsoft.Owin.Security                                                                                  
Microsoft.Owin.Security.Cookies                                         
Microsoft.Owin.Security.Facebook                                                                      
Microsoft.Owin.Security.Google                                                                             
Microsoft.Owin.Security.Jwt                                                                                   
Microsoft.Owin.Security.Microsoft                                                                     
Microsoft.Owin.Security.OAuth                                                                     
Microsoft.Owin.Security.Twitter                                                                        
Microsoft.Owin.SelfHost                                                                                            
Ninject.Web.Common.OwinHost                                                                                                        
Ninject.Web.WebApi.OwinHost                                                                                         
Owin      

So, could someone please give me a hint about what I'm doing wrong. 因此,有人可以给我提示我做错了什么。

public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
        //Other action methods were omitted 
    }             

Updates: added OWIN Startup Attribute. 更新:添加了OWIN启动属性。

I think you are missing at least one NuGet package. 我认为您至少缺少一个NuGet软件包。 You probably need Microsoft.AspNet.WebApi.Core and you might want Microsoft.AspNet.WebApi.Client to handle content negotiation. 您可能需要Microsoft.AspNet.WebApi.Core,并且可能需要Microsoft.AspNet.WebApi.Client处理内容协商。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM