Redirect in Razor pages with custom PageRouteModel RRS feed

  • Question

  • User749918359 posted


    I made a custom convention for my razor pages so that the url of every pages looks like : https://localhost/somekey/Index instead of https://localhost/Index

    Because of that, I can't make links work in the razor pages

        public class CustomPageRouteModelConvention : IPageRouteModelConvention
            public void Apply(PageRouteModel model)
                foreach (var selector in model.Selectors.ToList())
                    var template = selector.AttributeRouteModel.Template;
                    // insert the buyer before the default route
                    selector.AttributeRouteModel.Template = "{SomeKey}/" + template;

    The "{SomeKey}" can be a random string but it's not an existing folder.
    Here, Privacy.cshtml is located in Pages/Privacy

                                <a class="nav-link text-dark" asp-area="" asp-page="/Privacy/">Privacy</a>

    This link won't work because of the {SomeKey} parameter but I can't figure how to make it work.

    Monday, June 29, 2020 11:35 AM

All replies

  • User-17257777 posted

    Hi Etrigger,

    The "{SomeKey}" can be a random string but it's not an existing folder.

    A little confused, what's the value of SomeKey, I think you need to assign a value to it.

    Best Regards,

    Jiadong Meng

    Tuesday, June 30, 2020 9:49 AM
  • User749918359 posted

    Hi jiadongm,

    Thanks for you answer.

    Somekey will always have a value.
    Here are some url exemples :
    - localhost/somekey-a/Client/Home
    - localhost/a_random_string/Client/Home
    - localhost/another_random_string/Client/Home
    Those url will all access the same page located in Pages/Client/Home.cshtml

    I made some tests and
    -<a href="/somekey-a/Client/Home"></a> work
    -<a asp-page="/somekey-a/Client/Home"></a> doesn't work
    -<a asp-page="/Client/Home"></a> doesn't work
    -<a asp-route="/Client/Home"></a> doesn't work
    -<a asp-page="./Client/Home"></a> doesn't work

    The problem seems to come from the tag helper.

    I do this so that I can get the value of {somekey} with routedata but {somekey} must be inserted between the root of the url and the virtual path of the pages.

    Wednesday, July 1, 2020 8:37 AM