Jim S
Jim S

Reputation: 1079

Add Razor Page' Dialog does not allow hyphens in a filename

From the references below, it seems that there has been an ongoing problem with hyphens in the filenames for Razor Pages. The bugs below have been documented as fixed, but Add Razor Page still does not permit hyphens, as shown below: Error message

My SEO guy insists that filenames must have hyphens, and if I rename a file later to put a hyphen in, it seems to work fine.

Am I correct in thinking this is a bug? Where should I report it?

References:

https://github.com/aspnet/Mvc/issues/6296

RazorPages with filenames that include a hyphen cause IntelliSense to break

Using dash/hyphen in Razor Page filename - does compile but VS shows errors

Upvotes: 2

Views: 947

Answers (2)

BillRob
BillRob

Reputation: 4858

You can use the @page directive to explicitly give the route name.

@page "/multi-named/edit-me"
@model EditMeModel
@{
}

Upvotes: 3

Mike Brind
Mike Brind

Reputation: 30045

Rather than try to battle with a bug or whatever it is, you could use an underscore where the hyphen should go and then use an IPageRouteModelConvention implementation to replace the underscore with a hyphen for routing purposes, keeping your SEO guy happy.

A suitable implementation might look like this:

public class HyphenPageRouteModelConvention : IPageRouteModelConvention
{
    public void Apply(PageRouteModel model)
    {
        foreach (var selector in model.Selectors.ToList())
        {
            selector.AttributeRouteModel.Template = selector.AttributeRouteModel.Template.Replace("_","-");

        }
    }
}

Then you just need to register that at Startup:

services.AddMvc().AddRazorPagesOptions(options =>
{
    options.Conventions.Add(new HyphenPageRouteModelConvention());
}).SetCompatibilityVersion(CompatibilityVersion.Latest);

You can read more about this interface here: https://www.learnrazorpages.com/advanced/custom-route-conventions

Upvotes: 4

Related Questions