为了更好地测试Microsoft.Owin.Testing.TestServer,我发现Global.asax没有加载owin TestServer.

所以,我试着把我的全球.尽快启动.cs如下:,

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // pasted Global.asax things start.
        GlobalConfiguration.Configuration.Formatters.Clear();

        var jsonSerializerSettings = new JsonSerializerSettings
        {
            PreserveReferencesHandling = PreserveReferencesHandling.Objects,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
        };
        GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter() { SerializerSettings = jsonSerializerSettings });
        GlobalConfiguration.Configuration.Formatters.Add(new FormMultipartEncodedMediaTypeFormatter());

        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        // pasted Global.asax things end.

        ConfigureAuth(app);
    }
}

但在配置的每个点,比如AreaRegistration.RegisterAllAreasFilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)等,都需要初始化TestServerfailed...

对于我来说,最低可行的迁移(使用TestServer成功测试)如下所示.

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var config = new HttpConfiguration();
        config.Formatters.Clear();

        var jsonSerializerSettings = new JsonSerializerSettings
        {
            PreserveReferencesHandling = PreserveReferencesHandling.Objects,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
        };
        config.Formatters.Add(new JsonMediaTypeFormatter() { SerializerSettings = jsonSerializerSettings });
        config.Formatters.Add(new FormMultipartEncodedMediaTypeFormatter());

        WebApiConfig.Register(config); // moved from GlobalConfiguration.Configure(WebApiConfig.Register)
        app.UseWebApi(config);
        ConfigureAuth(app);
    }
}

是否可以将所有配置移动到Startup.cs?

推荐答案

正如您已经知道的,Startup.Configuration()消耗的OwinContext不同于传统的ASP.净消耗量为HttpContext,消耗量为MvcApplication.Application_Start().两者都使用不同的上下文管道.更具体地说,ASP.NETMVC仍然依赖于System.Web.dll,而ASP.NetWebAPI没有.

因此,根据您的代码,一些通常放在MvcApplication.Application_Start()中的方法不能在Startup.Configuration()中运行:

  • AreaRegistration.RegisterAllAreas();:这个方法依赖于System.Web.dll.
  • RouteConfig.RegisterRoutes(RouteTable.Routes);:RouteCollectionSystem.Web.dll的一部分.
  • GlobalConfiguration.Configure(WebApiConfig.Register):同样,WebApiConfig.Register()中的RouteCollectionSystem.Web.dll的一部分.

对于OWIN上下文中的URL路由,建议使用AttributeRouting.所以,与其这样,不如试试config.MapHttpAttributeRoutes();,那样会给你更多自由.

如果您仍然想在OWIN上下文Startup.Configuration()中运行AreaRegistration.RegisterAllAreas();,我建议您导入Katana library.这将OWIN和System.Web.dll结合起来,这样你就有可能实现你的目标.

Asp.net相关问答推荐

asp.net 单选按钮分组

Azure 自定义控制器/API .Net 后端

使用 jQuery.ajax() 时如何处理错误?

在 RedirectToAction 调用中传播 QueryString 参数

Devexpress 或 Telerik Controls 比较

发布发布事件

Web Api 参数始终为空

如何使用 app_GlobalResource 或 app_LocalResource?

Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用

使用 JObject 所需的库名称是什么?

将通用列表绑定到转发器 - ASP.NET

将数据表导出到 Excel 文件

LINQ:使用 Lambda 表达式获取 CheckBoxList 的所有选定值

在asp.net mvc 3中实现FilterAttribute,IActionFilter和从ActionFilterAttribute继承有什么区别?

IIS Express - 增加内存限制

*.csproj 中的 usevshostingprocess 是什么?

缩小 ASP.Net MVC 应用程序的 HTML 输出

ASP.NET - 从静态方法/静态类访问会话?

Application_End global.asax

如何将 DataTable 转换为类 Object?