有没有办法对Razor Pages进行动态路由? [英] Is there a way to do dynamic routing with Razor Pages?
本文介绍了有没有办法对Razor Pages进行动态路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找类似的东西
app.UseEndpoints(endpoints =>
{
endpoints.MapDynamicControllerRoute<MyTransformer>($"{store}/{controller}/{action}");
});
,但用于剃须刀页面路由.我通过AddRazorPagesOptions约定进行了挖掘,但似乎没有什么脱颖而出的.检查了Github和Google.那里也找不到很多.
but for the razor pages routes. I dug though the AddRazorPagesOptions Conventions but nothing seemed to stand out. Checked Github and Google. Didn't find much there either.
推荐答案
回答我自己的问题.看起来好像有一个与Razor Pages一起使用的MapDynamicPageRoute.您只需要使用页面"键而不是控制器"和动作"即可.
Answering my own question. Looks like there is a MapDynamicPageRoute that works with Razor Pages. You just have to do use the "page" key instead of "controller" and "action".
public class MyTransformer : DynamicRouteValueTransformer
{
public MyTransformer()
{
}
public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext
httpContext, RouteValueDictionary values)
{
return await Task.Run(() =>
{
var rng = new Random();
if (rng.NextDouble() < 0.5)
{
values["page"] = "/MyRazorPageA";
}
else
{
values["page"] = "/MyRazorPageB";
}
return values;
});
}
}
您还必须向MapDynamicPageRoute注册转换器端点,例如
You also have to register the transformer endpoint with MapDynamicPageRoute like
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapDynamicPageRoute<MyTransformer>("rando/{path?}");
});
这篇关于有没有办法对Razor Pages进行动态路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文