为特定的 Razor Pages 路由调用特定的过滤器?
Call particular filter for particular Razor Pages Route?
我有 2 个域(.com
和 .ru
)和 2 个 URL,例如 site.com/about-us
和 site.ru/o-nas
,应该是 重定向到同一页面。该站点使用 Razor Pages。
此外,特定的 URL 应该在适当的域中可用。例如:
site.COM/o-nas
不应该工作并且 return Not Found (404)
site.RU/about-us
不应该工作并且 return Not Found (404)
我发现过滤器工作正常,但对于 site.com/about-us
和 site.ru/o-nas
两个过滤器都被调用。
如何为特定的 URL 只调用 1,可能吗?谢谢,我当前的代码如下。
public static class DomainFilters
{
public static IPageApplicationModelConvention DomainEng(
this PageConventionCollection con, string pageName, string route = "")
{
return con.AddPageApplicationModelConvention(pageName, model =>
{
model.Filters.Add(new EnglishActionFilter(route));
});
}
public static IPageApplicationModelConvention DomainRussian(
this PageConventionCollection con, string pageName, string route = "")
{
return con.AddPageApplicationModelConvention(pageName, model =>
{
model.Filters.Add(new RussianActionFilter(route));
});
}
}
public class EnglishActionFilter : IResultFilter
{
public void OnResultExecuting(ResultExecutingContext context)
{
if (context.HttpContext.Request.Host.ToString().Contains(".ru"))
{
context.Result = new NotFoundResult();
}
}
public void OnResultExecuted(ResultExecutedContext context) { }
}
public class RussianActionFilter : IResultFilter
{
public void OnResultExecuting(ResultExecutingContext context)
{
if (context.HttpContext.Request.Host.ToString().Contains(".com"))
{
context.Result = new NotFoundResult();
}
}
public void OnResultExecuted(ResultExecutedContext context) { }
}
最后 ConfigureServices
来自 Startup.cs
的方法:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.DomainEng("/AboutUs", "about-us");
options.Conventions.DomainRussian("/AboutUs", "o-nas");
})
}
考虑实施自定义 FilterFactory
:
public class LanguageFilterFactory : Attribute, IFilterFactory
{
public bool IsReusable => false;
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
{
var context = serviceProvider.GetService<IHttpContextAccessor>();
if (context.HttpContext.Request.Host.ToString().Contains(".com"))
{
return new EnglishActionFilter();
}
return new RussianActionFilter();
}
}
该工厂将创建 English
或 Russian
过滤器(取决于域)。这就是它的职责所在。其余的交给过滤器本身(您需要更改过滤器内的代码以使其验证页面定位器):
public class RussianActionFilter : IResultFilter
{
public void OnResultExecuting(ResultExecutingContext context)
{
// you may want to play with RouteData in order to make this check more elegant
if (context.HttpContext.Request.Path.Value.Contains("About"))
{
context.Result = new NotFoundResult();
}
}
}
过滤器工厂的应用方式与其他过滤器相同:
[LanguageFilterFactory]
public class IndexModel : PageModel
Startup.cs
文件更新:
.AddMvcOptions(options =>
{
options.Filters.Add<LanguageFilterFactory>();
});
我有 2 个域(.com
和 .ru
)和 2 个 URL,例如 site.com/about-us
和 site.ru/o-nas
,应该是 重定向到同一页面。该站点使用 Razor Pages。
此外,特定的 URL 应该在适当的域中可用。例如:
site.COM/o-nas
不应该工作并且 returnNot Found (404)
site.RU/about-us
不应该工作并且 returnNot Found (404)
我发现过滤器工作正常,但对于
site.com/about-us
和site.ru/o-nas
两个过滤器都被调用。
如何为特定的 URL 只调用 1,可能吗?谢谢,我当前的代码如下。
public static class DomainFilters
{
public static IPageApplicationModelConvention DomainEng(
this PageConventionCollection con, string pageName, string route = "")
{
return con.AddPageApplicationModelConvention(pageName, model =>
{
model.Filters.Add(new EnglishActionFilter(route));
});
}
public static IPageApplicationModelConvention DomainRussian(
this PageConventionCollection con, string pageName, string route = "")
{
return con.AddPageApplicationModelConvention(pageName, model =>
{
model.Filters.Add(new RussianActionFilter(route));
});
}
}
public class EnglishActionFilter : IResultFilter
{
public void OnResultExecuting(ResultExecutingContext context)
{
if (context.HttpContext.Request.Host.ToString().Contains(".ru"))
{
context.Result = new NotFoundResult();
}
}
public void OnResultExecuted(ResultExecutedContext context) { }
}
public class RussianActionFilter : IResultFilter
{
public void OnResultExecuting(ResultExecutingContext context)
{
if (context.HttpContext.Request.Host.ToString().Contains(".com"))
{
context.Result = new NotFoundResult();
}
}
public void OnResultExecuted(ResultExecutedContext context) { }
}
最后 ConfigureServices
来自 Startup.cs
的方法:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.DomainEng("/AboutUs", "about-us");
options.Conventions.DomainRussian("/AboutUs", "o-nas");
})
}
考虑实施自定义 FilterFactory
:
public class LanguageFilterFactory : Attribute, IFilterFactory
{
public bool IsReusable => false;
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
{
var context = serviceProvider.GetService<IHttpContextAccessor>();
if (context.HttpContext.Request.Host.ToString().Contains(".com"))
{
return new EnglishActionFilter();
}
return new RussianActionFilter();
}
}
该工厂将创建 English
或 Russian
过滤器(取决于域)。这就是它的职责所在。其余的交给过滤器本身(您需要更改过滤器内的代码以使其验证页面定位器):
public class RussianActionFilter : IResultFilter
{
public void OnResultExecuting(ResultExecutingContext context)
{
// you may want to play with RouteData in order to make this check more elegant
if (context.HttpContext.Request.Path.Value.Contains("About"))
{
context.Result = new NotFoundResult();
}
}
}
过滤器工厂的应用方式与其他过滤器相同:
[LanguageFilterFactory]
public class IndexModel : PageModel
Startup.cs
文件更新:
.AddMvcOptions(options =>
{
options.Filters.Add<LanguageFilterFactory>();
});