How to you list of all pages in a .NET Razor Pages application? - razor-pages

Is it possible to get a list of all pages in a .NET 5 Razor Pages application?
I have done it in MVC and Blazor before by getting a list of all types in the application and filtering it by their base types (Controller for MVC and ComponentBase for Blazor).
For Razor Pages you could do it by getting all types that implement PageModel, but that doesn't seem to work for pages that do not have a code-behind .cshtml.cs model file.

If you want to list all of the navigable pages, you can use the EndpointDataSource service. You can inject it into your PageModel constructor or directly into a Razor page. The following example returns the relative path of the page files, rooted in the Pages folder. The data is added to a Hashset to prevent duplications. There will be two endpoints for each Index file - one with a route template with /Index on the end and another with an empty string instead of /Index.
#using Microsoft.AspNetCore.Mvc.RazorPages
#using Microsoft.AspNetCore.Routing
#inject EndpointDataSource EndpointsDataSource
#{
HashSet<string> Pages = new HashSet<string>();
foreach (var endpoint in EndpointsDataSource.Endpoints)
{
foreach(var metadata in endpoint.Metadata)
{
if(metadata is PageActionDescriptor)
{
Pages.Add(((PageActionDescriptor)metadata).RelativePath);
}
}
}
}
<ul>
#foreach(var p in Pages)
{
<li>#p</li>
}
</ul>
If you want to get another property, documentation for the PageActionDescriptor type is here: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.razorpages.pageactiondescriptor
Also, if you wanted to get the route templates for each page, filter the metadata for types that are compatible with the PageRouteMetadata type e.g.
if(metadata is PageRouteMetadata)
{
Routes.Add(((PageRouteMetadata)metadata).PageRoute);
}

Related

AngularJS 2 with .NetFramework 4.6

I'm currently making a web application with Asp.net MVC 5 (.Net Framework 4.6) and AngularJS 2.0 .
I take care the back-end and my friend takes care the front-end.
Now, he sends me 3 js files, 1 html file.
Can anyone help me to import those file to MVC 5 please ?
Thank you,
I´m assuming you are new to MVC...
Create a new MVC project.
Copy your js files to Scripts. (Drag from explorer to visual studio)
The best way to go with js is to make a bundle:
In app_start, open BundleConfig.cs
Create a new Bundle like this:
bundles.Add(new ScriptBundle("~/bundles/mybundle").Include(
"~/Scripts/myjs1.js",
"~/Scripts/myjs2.js",
"~/Scripts/myjs3.js"));
Now to render this bundle, open Views->Shared->_Layout.cshtml
Go to the bottom, find #Scripts.Render("~/bundles/jquery")
Add your bundle: #Scripts.Render("~/bundles/mybundle") Now, your js is available to all pages.
We need to create a controller. Right click Controllers folder -> add -> Controller
Select MVC5 Controller Empty.
Give it the name you like for this page.
You will see this:
public class testeController : Controller
{
// GET: teste
public ActionResult Index()
{
return View();
}
}
11. Right Click the Index in the code -> click in add View
12. VS will open the view. Paste your HTML here.
You are good to go!
Things to keep in mind 1 - ROUTES:
MVC default route (URL) is /controller/Action/id
In the example above, your url will be: /teste or /teste/index because it´s the controllers name.
If not provided MVC uses Home as controller name and Index as action name and id is optional.
So, if your page is the home page for the site put it in the Views -> Home -> index.cshtml
Things to keep in mind 2 - BUNDLES:
Bundles are available for all pages, if you need your js in only one page, to this:
Open your View, go to the bottom of the file and do this:
#section scripts{
<script src="~/Scripts/js1.js"></script>
<script src="~/Scripts/js2.js"></script>
<script src="~/Scripts/js3.js"></script>
}
Things to keep in mind 2 - LAYOUT:
MVC works divides your content into 2 files, stuff common to all pages, like navigation bar, footer, css and js calls reside in:
Views -> Shared -> _Layout.cs
Views are the changeable content in the middle of the page, look for the method #RenderBody() in _Layout.cshtml to find where your view will be rendered.
Maybe you will have to divide your friend´s html to have some in _layout and some in the View. This is quite common.
Good luck and Happy Coding!!
Sure,
Create an ../app folder and drop the js files
The html file you have, I suggest you copy paste its content into a razor view, ../Views/App/MyPage.cshtml
Then create a c# controller that will return that view AppController
public class AppController : Controller
{
public ActionResult MyPage()
{
return PartialView();
}
Like this https://github.com/victorantos/AngJobs/blob/master/AngJobs/Controllers/AppController.cs
Another important note, make sure your router is configured properly, like this
https://github.com/victorantos/AngJobs/blob/master/AngJobs/App_Start/RouteConfig.cs

Sails js render partial view

I'm new in SailsJS so i have a question about rendering partial views.
I've came from .NET world and partial views in ASP.NET MVC are much more clever than in sails. In .NET MVC i can provide some controller as partial view path and this controller will be executed and partial view will be rendered with that controller data.
For example I want to show some account info in the corner of my website. So in .NET i can call partial view, providing controller path, this controller will query account info, provide it to partial view, and then this result will be inserted to main view.
In Sails i can call partial view, but all data (locals) for partial view will be pushed there from main view. So i have to put account information in every controller in application?
How can i solve this problem?
In other questions i've found that I can take username from req.session but it does not solve all cases. How can i show three random topics in page footer for example?
I think you are looking at the wrong place about the partial views.
This is a front-end problem .What I use for such partial views is Angular UI-router . Actually I am not sure if using angular is in the scope of the solution for you,So I'll keep this short . If you feel my method is useful ,you can comment it ,and I'll elaborate further on how to do it .
Depends on the template engine, that you use.
With ejs you can use <%- include('someview') %> or <%- include('someview', { mydata: { foo: bar} }) %>
The latter allows the include file to be called several times with diff data. E.g. <%- include('someview', { mydata: foo }) %> will copy foo (from locals) to the mydata key. This will be added to locals, so all other locals are available too.
As for providing the actual data, you can use a sails service. Just add data to res.locals.myKeyForTheInclude.
You will have to call the service each time.
But you can add it in config/http as a middleware.
middleware: {
myFoo: myService.myFunc,
order: [
...
'myFoo', // before router
'router',
...
],
All that is left is that each template needs the include.
You can not avoid that, only the template knows where the include should be.
But if you use a layout, that maybe it fits into the layout.

Dynamic Menu Navigation In Umbraco Using .Net User Control

I am .Net developer and am looking for example related to creation of dynamic navigation menu in umbraco 7 using .Net user control without using MVC technique and XSLT. I have searched it on google but got not much response on it, examples which i got were using either XSLT or Razor. I have not any experience in MVC and XSLT and looking for technique related to using classic ASP.Net, i.e. without using XSLT and MVC. It will a great help if any one could please provide some useful videos or links with examples which i could refer to.
Thanks
Tarunjit Singh
You should really look into the Razor thing. It just TOO simple not to try!
It looks like c# mixed with some HTML. If you really want to stick to .Net UserControls you might miss out on future versions.
Creating navigation in a usercontrol is very difficult because you have 2 options
you need to write out HTML yourself
create some custom model to some 3th party control
Both of these options will never return the exact html as you wanted. Razor (and xslt) on the other hand will.
If you still want try something in a .Net User control anyway, get an instance of the Umbraco Nodes you want. Let's give an example:
using System;
using System.Linq;
using Umbraco.Web;
public partial class UserControls_TestUserControl : UmbracoUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var cache = this.UmbracoContext.ContentCache;
var rootNodes = cache.GetAtRoot().First().Children;
foreach (var node in rootNodes)
{
Response.Write("<li>" + node.Name + "</li>");
}
}
}
If you want to call this code, don't forget to add a macro, and insert the macro in your template.
Just in case you would like to explore razor, here is an equivalent:
#inherits UmbracoTemplatePage
#{
var homePage = CurrentPage.AncestorOrSelf(1);
var menuItems = homePage.Children;
}
<nav>
<ul>
<li>Home</li>
#foreach (var item in menuItems)
{
<li>#item.Name</li>
}
</ul>
</nav>
This example can be used as a partial view. And can be put in your (Razor) template using: #Html.Partial("NameOfThePartial")
You have to admit, it's not too hard.

Exception trying to load tab from ajax request using Ext.NET

I'm using asp.net mvc with ext.net and I'm trying to create a set of tabs that load information only when they are selected by the user.
I can manage to load a partial view into a tab/panel using the ContentFromAction functions:
But I can't figure out how to populate a tab/panel only when a tab is selected.
I've based my project on the Ext.NET MVC Examples Explorer version 2.5 code base and this code on the TabPanel > Basic > Ajax Load example found here
I've cut down the example as far as I can to reproduce the problem:
In my view I create the tab and configure the loader (exactly the same as the example project):
Index.cshtml
X.Panel()
.ID("Tab3")
.Title("Ajax Tab")
.BodyPadding(6)
.AutoScroll(true)
.Loader(X.ComponentLoader()
.Url(Url.Action("Ajax"))
.LoadMask(m => m.ShowMask = true)
.Params(new Parameter("containerId", "Tab3"))
.Mode(LoadMode.Html)
)
It correctly calls into my controller (exactly the same as the example project):
Axax_LoadController.cs
using System.Web.Mvc;
namespace Ext.Net.MVC.Examples.Areas.TabPanel_Basic.Controllers
{
public class Ajax_LoadController : Controller
{
public ActionResult Ajax(string containerId)
{
return View("Ajax");
}
}
}
Which in turn displays the appropriate view in the tab after it's been clicked on:
Ajax.cshtml (this works)
#using Ext.Net.MVC
<div>
<p>I am content loaded via Ajax when the tab is selected</p>
</div>
The problem begins if I try to add controls in my view, as follows:
Ajax.cshtml
#using Ext.Net.MVC
#{ var X = Html.X(); }
<div>
<p>I am content loaded via Ajax when the tab is selected</p>
#X.TextField().Text("I am a text field")
</div>
This fails with the exception:
ItemTag validation (_tkn_1): Reference token (init_script) was not found.
If I modify the file Ext call to return Html as follows:
Ajax.cshtml
#using Ext.Net.MVC
#{ var X = Html.X(); }
<div>
<p>I am content loaded via Ajax when the tab is selected</p>
#X.TextField().Text("I am a text field").ToHtmlString()
</div>
It correctly renders the following text in my selected tab:
I am content loaded via Ajax when the tab is selected
<#:item ref="init_script" index="0">Ext.create("Ext.form.field.Text",{renderTo:"App.id534c5fe0f159f3fb_Container",value:"I am a text field"});</#:item><div id="App.id534c5fe0f159f3fb_Container"></div>
I believe that the ext.net code is written by #geoffrey.mcgill on stack overflow so I'm hoping he can help rescue me.
You need to use a PartialViewResult. Please look at these examples.
Partial Content
Partial Items
Personally, I would recommend to follow the Partial Items example. You always can wrap any non-Ext.NET content in an Ext.NET Container. The benefit of this approach is the fact that you don't need to worry about destroying Ext.NET components if you reload the content. Though, anyway, I would recommend to set up explicit IDs for Ext.NET components in a partial view. At least, for top level components.

Liferay - Getting localized name in javascript

Consider the following code
function populateLayout(scopeGroupId){
Liferay.Service.Portal.Layout.getLayouts(
{
groupId: scopeGroupId,
privateLayout: false
},
function(layouts){
for(var i=0;i<layouts.length;i++){
var layout = layouts[i];
alert(layout.name);
}
}
);
}
As you can see from the above Liferay's JSON service API to get all the layouts. I particularly need layout name to populate in a select box. I know that the name is stored as xml string to support different locales. I was wondering if there is javascript api to get just the name of the layout using this xml string and language id. There is a java api for doing the same as below. I need equivalent javascript API if any.
layout.getName(locale)
or
LocalizationUtil.getLocalization(String xml, String languageId)
I believe there is no such utility on Liferay's JavaScript libs.
Most of the times people need to do it in Liferay, they just parse the content in the server side and send the parsed content to the client. Frequently, the client only needs the value in the default locale; if it needs more, the developer finds a way to write it in HTML itself. See e.g. the input-localized tag where the developer writes the code to create a JS object directly in the output.
This makes sense: parsing XML is resource-costly so one would rather avoid it on client-side JavaScript. It may be even that there is a utility to do that in Liferay but I'd propose an alternative approach: just ask what you want to use in your JavaScript and write it in the HTML. For example, instead of writing the following JavaScript code
var names = Liferay.Util.Localization.nonExistentMethodToGetLocaleMap(layout.name);
for (var locale : names) {
alert(names[locale]);
}
decide in server-side which layouts to display and write the following JSP code:
Map<Locale, String> names = layout.getNameMap();
<script>
<% for (String name : names.values()) { %>
alert('<%= name %>');
<% } %>
</script>
Not much clear, I agree, but rather efficient.

Resources