Include controller and model source code of Razor Class Library - asp.net-core

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.

Related

How to disable Overriding Razor class library views

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.

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?

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

Use Partial Views between multiple mvc application

I have an scenario like, define a template for footer of my website in partial view. I have written the footer template in partial view. This partial view is placed in folder name "Common" in Views.
Now i want to use this footer partial view in another mvc application, because used the same footer template.
How can i refer the partial views in multiple applications, like having the partial views in class library?
Please guide me to go right way.
Regards,
Karthik.

How to expose and call methods on MVC 4 user controls

I am converting my asp.net site to MVC 4. My site has a control called loginbox that prompts the user for username and password. The control also exposes a method called IsLoggedIn that a hosting page can call. I want to continue to encapsulate the login logic in my loginbox control and call it in a similar fashion from a parent level page (i.e loginBoxInstance.IsLoggedIn()). How do I do that?
MVC doesn't have a concept of user controls. The whole setup of MVC is to separate logic from the view. You could achieve a similar setup by creating a separate controller and a partial view.
Then in your main view you could call RenderAction on the controller, which renders the partial view. However, this is only valid for the rendering stage, so something like IsLoggedIn() is not something you can (or should) do in MVC.
Example:
Controller
public class LoginController
{
public ActionResult Login()
{
return PartialView();
}
}
Partial View
// Place this file in Views/Login/Login.cshtml
<div>
<!-- Your markup -->
</div>
Main view
#Html.RenderAction("Login", "Login")
This will allow you to separate the view part (and also the logic) of the login rendering into a separate controller and view, which can be included in another view.
However, what you probably want is something like Action-attributes or inherit from a base controller class which handles all this for you.
The paradigm of MVC versus Web Forms is very different, and I think you should look into a more appropriate way of doing this.