How to disable Overriding Razor class library views - asp.net-core

I am now using ASP.Net Core to write a web application and relative Razor Class Libraries.
I know a view (*.cshtml) defined in a Razor Class Library can be override in web application. It brings enough flexibility to developers. But I feel sometimes the developer of a RCL doesn't expect that the view defined in RCL would be override.
Is there a way to disable overriding view (all or some specific views)?
Thanks!

I'm afraid you can't achieve this. The project will auto override the view. The Microsoft document has explian this.
When a view, partial view, or Razor Page is found in both the web app
and the RCL, the Razor markup (.cshtml file) in the web app takes
precedence.
When a view, partial view, or Razor Page is found in both the web app
and the RCL, the Razor markup (.cshtml file) in the web app takes
precedence. For example, add
WebApp1/Areas/MyFeature/Pages/Page1.cshtml to WebApp1, and Page1 in
the WebApp1 will take precedence over Page1 in the RCL.

Related

How to extract views from Razor class libraries?

When you add Identity to your ASP.NET Core applications, you can scaffold its Razor pages.
I am looking for a way to extract my views from a referenced RCL just like Identity.
I mean I want to create Area and related files (cshtml) to help developers to customize it (not a specific UI as you see in the above pic).
Do there exist any command-line tools to a way to extract Razor pages (.cshtml)?
I checked inside of a Razor class library with dotpeek there is no HTML resource, everything is class.
Any idea?

Best Practice to display text label on a .NetCore Razor page

Is it best practice for ALL text on a .Net Core Razor webpage to be injected with Model Binding (Even on a static page), or should I only inject text which may need to change dynamically at runtime?
E.g. My Index.cshtml page has a h1 title as per below. Is this considered bad practice or is it ok?
<h1 class="block-title-text" localize-content>A Fun Title</h1>
Thanks. Just trying to get my head around Razor and .Net Core.
This if fine, in general you should keep user interface elements in the view (or the .cshtml file for Razor Pages). See benefits of using views for more details, which among other things includes:
The parts of the app are loosely coupled. You can build and update the app's views separately from the business logic and data access components. You can modify the views of the app without necessarily having to update other parts of the app.
Just realised something which is obvious in hindsight. By injecting the strings using Model Binding instead of placing them into the HTML as above, it allows the incorporation of unit tests around those strings.

what is the difference between razorview and razorlayout in aspnet core

I used razor pages and want to create a razor page partial view.
I saw two options one razor view and another razor layout, what is different between the two? Which is better for a partial view?
It doesn't really matter which template you use to create a partial for Razor Pages. In both cases, you will need to replace the existing content and rename the file. A partial is just a single Razor file that doesn't have an #page directive.
You could also use the ViewStart and View Imports templates. The Razor Page template could also be used, but you would also need to delete the PageModel class file that gets generated.
More about Partials in Razor Pages here: https://www.learnrazorpages.com/razor-pages/partial-pages

Include controller and model source code of Razor Class Library

Is it possible to view and include the source code for the controller and models of the Identity package in Asp.Net Core 3 into my VS project?
I have scaffolded the package but it currently shows only the view files.
There is no controller or model code. The Identity default UI uses Razor Pages. With Razor Pages, there's a code-behind (.cshtml.cs), which has methods similar to controller actions, called handlers, and the model is actually a PageModel and is the entire backing class for the view.
The views should have a little arrow to the left of them, which you can expand to show the code-behind.

VS 2017 ASP.NET Core Web Application and Individual User Accounts works but pages are non-existent

When I create a web project using "ASP.NET Core Web Application" / "Web Application" (non-MVC) / "Individual User Accounts"... I get the sample project with Register and Login buttons that show their respective pages when I click on them. HOWEVER... the very weird thing is if I search for "Create a new account" which is on the register page it's not found... and more... there is no AccountController in the Identity area or anywhere... so 1) the pages aren't anywhere in my solution folder and 2) how in the world are these pages showing up?!? I've tried on two different computers now. (using Core 2.1)
As an example this link works:
<a asp-area="Identity" asp-page="/Account/Login">Login</a>
and takes me to:
https://localhost:44300/Identity/Account/Login
and yet my folder structure (both in the solution and on disk) looks like this:
ASP.NET Core 2.1 with Identity automatically includes the "Default UI". It's a Razor Pages class library that's referenced via AddDefaultIdentity in Startup, which under the hood literally calls AddDefaultUI.
If you want the actual files in your project, you need to scaffold them in. Right click your project in the solution explorer and choose Add > New Scaffolded Item... Then, click the Identity listing on the left, and then the Add button. A new window will pop allowing you to select the pages you want included, your context, etc. Configure it how you like and go.
It's also worth noting, that as long as you use AddDefaultIdentity, the default UI is still included, which means you don't actually need all the scaffolded files if you want any of them. They'll essentially function as overrides. Anything specifically included in your project will be used, while anything that's missing will be pulled from the default UI.
This also means that if you want to do something like use standard controller actions and views instead of Razor Pages, the default UI will still be active, and take precedence. You have to use AddIdentity or AddIdentityCore instead of AddDefaultIdentity if you want the default UI off completely.
ASP.NET Core 2.1 introduced Razor UI in class libraries. Identity with it's UI is in nuget package.
The nice thing about this is you can now package UI with your libraries and then expose the UI for overriding.
This post describes how to override the UI. Essentially, you add the pages you want to override to your project to /Areas/Identity/Pages.... the easiest way currently is to run the scaffolder for identity and a visual walkthrough.