Reputation: 764
I'm playing with asp.net razor pages.
I've created default asp.net core 2.1 application then added 3 pages:
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
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
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