首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IApplicationBuilder映射分支不像ASP.NET 5中期望的那样工作

IApplicationBuilder映射分支不像ASP.NET 5中期望的那样工作
EN

Stack Overflow用户
提问于 2015-07-25 17:53:17
回答 1查看 3.3K关注 0票数 2

我正在尝试基于请求url的第一个文件夹段来实现多租户。

请考虑我的启动过程中的以下片段。

代码语言:javascript
复制
foreach (SiteFolder f in allFolders)
{
    PathString path = new PathString("/" + f.FolderName);
    app.Map(path,
    siteApp =>
    {
        //here I'm trying to specify a different auth cookie name for folder sites and some other configuration
        // but problem happens even with no code here at all
        // I also tried adding the folder specific routes here but still 404
    });
}

app.UseMvc(routes =>
{
    List<SiteFolder> allFolders = siteRepo.GetAllSiteFoldersNonAsync();

    foreach (SiteFolder f in allFolders)
    {
        routes.MapRoute(
        name: f.FolderName + "Default",
        template: f.FolderName + "/{controller}/{action}/{id?}",
        defaults: new { controller = "Home", action = "Index" },
        constraints: new { name = new SiteFolderRouteConstraint(f.FolderName) }
    );

    routes.MapRoute(
        name: "default",
        template: "{controller}/{action}/{id?}",
        defaults: new { controller = "Home", action = "Index" });
});

如果我在顶部注释掉了app.Map,那么我的路由就会像预期的那样工作,但是如果我在与文件夹路由相同的文件夹名上分支,文件夹urls都会导致IIS404页面。

我想也许siteApp没有看到添加到应用程序中的路由,所以我尝试将这些路由添加到siteApp中,但结果仍然是404。

在MVC 5中,类似的方法也适用于我,我觉得框架(beta5)中要么有一个bug,要么我忽略了一些关于分支和新框架的重要概念。

EN

回答 1

Stack Overflow用户

发布于 2015-07-25 23:17:16

这正是Map在Katana中的工作方式,并且应该在ASP.NET 5中工作。

Map需要注意的最重要的一点是,当请求路径对应于注册路径时,总是终止请求。换句话说,当路径匹配path参数时,在app.Map调用后注册的中间件永远不会被执行,这就是为什么您的app.UseMvc中间件从未在您的配置中执行过。

由于您不处理app.Map委托中的请求,因此将执行默认处理程序并应用404响应。

您应该尝试一下这种扩展方法。与app.Map不同,它从不停止处理请求,基本上启用了我称之为“条件中间件处理”的场景:

代码语言:javascript
复制
public static IApplicationBuilder UseWhen(
    [NotNull] this IApplicationBuilder app,
    [NotNull] Func<HttpContext, bool> condition,
    [NotNull] Action<IApplicationBuilder> configuration) {
    var builder = app.New();
    configuration(builder);

    return app.Use(next => {
        builder.Run(next);

        var branch = builder.Build();

        return context => {
            if (condition(context)) {
                return branch(context);
            }

            return next(context);
        };
    });
}

它是专门为AspNet.Security.OpenIdConnect.Server示例开发的,但是您当然可以在任何地方使用它。

你可以在那里看到它的作用:https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/blob/vNext/samples/Mvc/Mvc.Server/Startup.cs#L46-L80

代码语言:javascript
复制
// Create a new branch where the registered middleware will be executed only for API calls.
app.UseWhen(context => context.Request.Path.StartsWithSegments(new PathString("/api")), branch => {
    branch.UseOAuthBearerAuthentication(options => {
        options.AutomaticAuthentication = true;
        options.Audience = "http://localhost:54540/";
        options.Authority = "http://localhost:54540/";
    });
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31629432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档