A K
A K

Reputation: 764

Create an alias for razor page

I'm playing with asp.net razor pages.

I've created default asp.net core 2.1 application then added 3 pages:

enter image description here

By default convention pages will available in path:

Page1 -> http://localhost/Page1
Page2 -> http://localhost/Folder/Page2
Page3 -> http://localhost/Page3

And now I want to add an alias for page2, e.g. something like this:

Page1 -> http://localhost/Page1
Page2 -> http://localhost/Page2
Page2 -> http://localhost/Folder/Page2
Page3 -> http://localhost/Page3

Is it possible to create additional route/alias for this page without adding-file-as-link?

Specifying page route in @page

@page "/Page2"
@{
    ViewData["Title"] = "Page2";
}

<h2>Page2</h2>

makes page2 available as http://localhost/Page2, but not as http://localhost/Folder/Page2 :(

Upvotes: 4

Views: 1653

Answers (2)

Kirk Larkin
Kirk Larkin

Reputation: 93133

You can achieve this with AddPageRoute(...). Here's an example for your situation:

services
    .AddMvc()
    .AddRazorPagesOptions(options =>
    {
        options.Conventions.AddPageRoute("/Folder/Page2", "/Page2");
    });

Upvotes: 4

Luke Narramore
Luke Narramore

Reputation: 509

This, may help. https://github.com/T4MVC/T4MVC

T4MVC is a T4 template for ASP.NET MVC apps that creates strongly typed helpers that eliminate the use of literal strings in many places.

It will generate literal paths for your views, then you could return different views from the same action.

if (someCondition){
    return View(this.ViewNames.Page2)
}
else {
    return View(this.ViewNames.Folder_Page2)
}

Upvotes: 0

Related Questions