Owin在web.config中提供启动类(没有自动启动发现)

时间:2015-08-11 12:00:35

标签: c# asp.net owin

我尝试在web.config中执行以下操作:

<appSettings>
   <add key="owin:AutomaticAppStartup" value="false" />
   <add key="owin:appStartup" value="MyNamespace.MyStartupClass" />
</appSettings>

如果我理解这个documentation,应该禁用正确的自动启动检测。所以我不需要启动属性。

不幸的是,看起来OWIN无法启动。 (我看到这个是因为我收到错误:HTTP Error 403.14 - Forbidden。我使用控制器来处理对索引文件的请求。)

如果我使用<add key="owin:AutomaticAppStartup" value="true" />并添加启动属性[assembly: OwinStartup(typeof(MyStartupClass))],则应用程序会按预期启动。

所以问题是为什么?我该怎么做才能解决这个问题?

我正在使用OWIN 3.0.0.0

更新

这是我的启动类的样子(带有相关部分的缩小版):

using System.Web.Http;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Owin;
using MyOtherNamespace;

namespace MyNamespace
{
    public class MyOnlineStartup : MyOtherStartup
    {
        public new void Configuration(IAppBuilder app)
        {
            base.Configuration(app); //Call base method! This is important because otherwise ther serilization will not be correct
            HttpConfiguration config = CreateRouting();
            config.Routes.MapHttpRoute("exampleAppNone", "", new { controller = "MyIndex" }, null, null);
            config.Routes.MapHttpRoute("exampleAppIndex", "index.html", new { controller = "MyIndex" }, null, null);
            app.UseWebApi(config); // Use the WebAPI technology.
        }
    }
}

它来源于

using System.Linq;
using System.Web.Http;
using Microsoft.AspNet.SignalR;
using Newtonsoft.Json;
using Owin;

namespace MyOtherNamespace
{
    public class MyOtherStartup
    {
        protected static HttpConfiguration CreateMyRouting()
        {
            HttpConfiguration config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                "myIndex",
                "my/",
                new
                {
                    controller = "MyIndex"
                },
                null,
                null
                );
            config.Routes.MapHttpRoute(
                "myIndex2",
                "my/index.html",
                new
                {
                    controller = "MyIndex"
                },
                null,
                null
                );
            var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
            config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
            config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto
            return config;
        }

        public void Configuration(IAppBuilder app)
        {
            JsonSerializer serializer = Serialization.ClientJsonSerializer();
            serializer.ContractResolver = new MySerializationContractResolver(false);
            GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => serializer);
            app.MapSignalR("/" + MyRequestHandler.MySignalRPath, new HubConfiguration());          
        }
    }
}

2 个答案:

答案 0 :(得分:27)

只需删除web.config文件中的以下代码行:

<add key="owin:AutomaticAppStartup" value="false" />

您的web.config文件现在必须如下所示:

<appSettings>
    <add key="owin:appStartup" value="MyNamespace.MyStartupClass" />
</appSettings>  

只需添加owin:appStartup密钥,您就不需要启动属性。

答案 1 :(得分:0)

有时,如果您重命名项目名称,那么也会出现此错误。请确保重命名您的程序集信息,还请检查您的项目属性。右键单击您的项目=>转到属性=>验证程序集名称和项目名称。

对我来说,这解决了我的OWIN库问题。

Project Properties Assembly Name Change