Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AddRazorPagesOptions Conventions.AddPageRoute中的通配符_Razor_Asp.net Core_Asp.net Core 2.0 - Fatal编程技术网

AddRazorPagesOptions Conventions.AddPageRoute中的通配符

AddRazorPagesOptions Conventions.AddPageRoute中的通配符,razor,asp.net-core,asp.net-core-2.0,Razor,Asp.net Core,Asp.net Core 2.0,我有以下代码: public void ConfigureServices(IServiceCollection services) { services .AddMvc() .AddRazorPagesOptions(options => { options.Conventions.AddPageRoute("/Index", "Index.html"); options.Conventions.Ad

我有以下代码:

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddMvc()
        .AddRazorPagesOptions(options => {
            options.Conventions.AddPageRoute("/Index", "Index.html");
            options.Conventions.AddPageRoute("/rt", "rt.html");
        });
}
有没有可能不写每一页,而是有一个这样的通配符路由

options.Conventions.AddPageRoute("/*", "/{*.html}");

没有内置的方法来添加这种通配符路由。但是,您可以通过简单的页面路由约定(实现
IPageRouteModelConvention
)来实现它。以下是一个工作示例:

public class HtmlExtensionPageRouteModelConvention : IPageRouteModelConvention
{
    public void Apply(PageRouteModel model)
    {
        var selectorCount = model.Selectors.Count;
        for (var i = 0; i < selectorCount; ++i)
        {
            var attributeRouteModel = model.Selectors[i].AttributeRouteModel;
            if (String.IsNullOrEmpty(attributeRouteModel.Template))
            {
                continue;
            }

            attributeRouteModel.SuppressLinkGeneration = true;
            model.Selectors.Add(new SelectorModel
            {
                AttributeRouteModel = new AttributeRouteModel
                {
                    Template = $"{attributeRouteModel.Template}.html",
                }
            });
        }
    }
}

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddMvc()
        .AddRazorPagesOptions(options => {
            options.Conventions.Add(new HtmlExtensionPageRouteModelConvention());
        });
}