我正在尝试基于请求url的第一个文件夹段来实现多租户。
请考虑我的启动过程中的以下片段。
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,要么我忽略了一些关于分支和新框架的重要概念。
发布于 2015-07-25 23:17:16
这正是Map在Katana中的工作方式,并且应该在ASP.NET 5中工作。
Map需要注意的最重要的一点是,当请求路径对应于注册路径时,总是终止请求。换句话说,当路径匹配path参数时,在app.Map调用后注册的中间件永远不会被执行,这就是为什么您的app.UseMvc中间件从未在您的配置中执行过。
由于您不处理app.Map委托中的请求,因此将执行默认处理程序并应用404响应。
您应该尝试一下这种扩展方法。与app.Map不同,它从不停止处理请求,基本上启用了我称之为“条件中间件处理”的场景:
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示例开发的,但是您当然可以在任何地方使用它。
// 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/";
});
});https://stackoverflow.com/questions/31629432
复制相似问题