ControllerActionInvoker & asp.net core Razor Pages - razor

The ControllerActionInvoker work with MVC controller to select an action. Is there something equivalent to choose a Razor Page between two pages?
I have two Razor Pages RazorPage1.cshtml and RazorPage2.cshtml. I want to use a logic to load either RazorPage1 or RazorPage2.

You can render then as Partial view base on you conditional logic in parent view
Or
In controller you can write a conditional logic to render different view based on view name

Related

How to render a view to html string in asp.net core controller?

In my asp.net core 2.0 application, I need to show the details of a payment receipt as a view to the user. I also need to send to user the same html text as an email (minus the top navigation bar).
My thought was to put the payment details in a partial view. I can then use this partial view on the website as well as in the email.
In appears Html object is injected into razor pages automatically such that one can, for example, call #Html.Partial(). Is there a way to use this Html object from the Controller class? Or, is there a different mechanism to render .cshtml file into html string? Regards.
You might try Microsoft.AspNetCore.Html.HtmlString or Microsoft.AspNetCore.Html.HtmlFormattableString. You can return these directly from the controller.

Thymeleaf automatic form generation

I'd like to create form in specific way. The form should be able to render itself based on received data, should be used like that:
<form action="#" th:action="#{/blahblah}" th:formDefinition="${formDef}" th:object="${formData}"method="post">
This should render the necessary input elements in some way, fill in the data etc. Is there a way how to achieve this in Thymeleaf?
Nope there is no such way in pure thymeleaf which you can do this.
But you have an option to do something like this using fragments.
Create a th:fragment which takes the parameters formDef and formData
In the fragment, create a loop and geenrate the form as you want dynamically
Call the fragment using th:include with the real parameters in the places where you need the form to get generated.
Use Apache Freemaker to create templates in Netbeans IDE. Using this templates, auto-generate your forms from your Entities. This allows you to apply the principle of DRY.....Dont Repeat Yourself in your SDLC.
For more infor, watch the below clip on how they have applied the principle:
https://nofluffjuststuff.com/blog/reza_rahman/2015/01/vaadin_cdi_and_java_ee

Plain html5 vs MVC razor security

I am not sure about it which one is more secure either plain Html 5 or MVC razor view. I guess perhaps it is MVC razor view as it encodes the string. Please suggest which is better way
What do you mean about security?
Razor - its engine which transform "template" to html. So thats mean if you print in razor template you will have it in output of html.
Well if we talk about cross side scripting first of all it depends how you do stuff in you application. But by default razor has fixed that. so lets say if you want print variable which contain HTMl it will escape that, but if you are sure that you want print raw you must use helper for that.
var html = "<script>alert(1)</script>"
#html //it will print "<script>alert(1)</script>"
#Html.Raw(html) will print what you want
So generally i would say that Razor "is a bit more secure" than plain html. So its not security its just one more layer

Wanting to insert cshtml inside cshtml in ASP.NET MVC4, similar to usercontrol in webforms

What is the best way to insert re-usable code into a cshtml razor file. I need to pass in data from the outer loop into the user control. I took a picture of what I'm trying to do hoping that will make it very clear.
Use
#Html.RenderPartial(<virtual path to your partial view>, session);
where partial view is typed view with the model of session type.

Correct way to use _viewstart.cshtml and partial Razor views?

I'm using _viewstart.cshtml to automagically assign the same Razor Layout to my views.
It's a dead simple file in the root of my Views folder that looks like this:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
This is more DRY than adding the #Layout directive to every single view.
However, this poses a problem for Razor partial views, because they run the contents of _viewstart.cshtml and therefore incorrectly assign themselves a layout, which makes them, um, no longer partial.
Here's a hypothetical project, showing the _viewstart.cshtml file, the shared _layout.shtml file, and a partial view ("AnonBar.cshtml").
Currently, the way that I'm getting around this is by adding the following line to every partial view:
#{
Layout = "";
}
This seems like the wrong way to denote a view as a partial in Razor. (Note that unlike the web forms view engine, the file extension is the same for partial views.)
Other options I considered but that are even worse:
Putting all partial views into a common folder, so they could share a common _viewstart.cshtml. This breaks the convention of views being in the same folder as their controller.
Not using partial views.
Is this something that is still being fleshed out by the Razor view engine team, or am I missing a fundamental concept?
If you return PartialView() from your controllers (instead of return View()), then _viewstart.cshtml will not be executed.