Preprocessing ASP.NET MVC Razor views - asp.net

I want to do some preprocessing on my views before they are parsed by the Razor template engine. The only way I found so far is by extending the RazorTemplateEngine class and overriding the CreateParser method, where I can return a custom parser that does the preprocessing before calling the base parser.
Now my problem is - how can I make Razor use my custom template engine?

I don't know if that will work for you but it should :)
try to add
#inherits YourRazorTemplateEngine
in the head of your views.
for more information look at this page

Related

is it possible to using razor class library for all web projects?

because of the ease of using razor class library and modularity, I decide to use it instead of the regular class library but the solution is, is it possible to write all core or
data layer code inside different razor class library
is it possible to write all core or data layer code inside different
razor class library
Please check the official document and this tutorial:
The Razor views, pages, controllers, page models, Razor components, View components, and data models can be built into a Razor class library (RCL). The RCL can be packaged and reused. Applications can include the RCL and override the views and pages it contains. 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.
So, you can write the Data layer code or logic in the Razor class library. But in my opinion, I suggest you could put the data layer code in the regular class library, then you could reuse them in other projects.

Where do I find the default MVC templates for Html.EditorFor?

I want to implement my own Html.EditorFor by creating a template in Views/Shared/EditorTemplates.
I would like to start with the vanilla template and change this to my needs.
But where can I see the default editor-for templates of ASP.net MVC?
What I try to do: get my own css classes (uikit instead of bootstrap) and a more simple way of adding validation with jquery.validate (using required attribute, for example).

Handlebars: Partials vs Helpers For Templating

We're starting to use Handlebars for the view layer in some of our projects. We are starting to hit a crossroads between two ways of doing some templating. I've been using partials to handle the templating & having small HTML templates like:
<p id="{{name}}">
<label for="{{name}}Input">{{text}}</label>
{{#if info}}
<small>{{info}}</small>
{{/if}}
<textarea name="{{name}}" id="{{name}}Input"></textarea>
</p>
Another developer feels that we shouldn't be using partials for this & instead we should be creating helpers for this.
I can see helpers being easier to handle input parameters (as I'm currently using some form of "include" helper to include these partials with some extra variables). But it doesn't sit right with me that you are writing HTML into strings in code - I don't think you're separating your concerns properly there. We are also using Handlebars in Java (via [Handlebars.Java][2]), so again your HTML is very much in compiled code - not in simple to edit view files.
Is there a generally accepted way to handle templating in Handlebars? Partials or Helpers or is there something else I don't know about?
Well, first you need to understand that partials are very different in handlebars.js opposed to handlebars.java
In handlebars.js, you declare your own partials and call/name them whatever you want in your controller (usually) and then call them within your view. In handlebars.java a partial is defined in your view and is essentially just an include taking a path attribute. Includes are generally integral to most projects and I don't think there's any benefit to dropping such an important piece of functionality.
Also, I've seen the mentality of "use helpers for everything" in many handlebars.js apps and it is becomes difficult to maintain very quickly. Helpers are a great feature but they should be used sparingly. Whenever possible, use the built in helpers and try structuring your data in a way that you doesn't require additional abstracted logic.
Looking at your example, i think thats exactly the correct useage of partial.

ASP.NET MVC 4 custom HTML Helpers folder location

I'm starting to develop an application using ASP.NET MVC 4 framework with Razor syntax. I want to know where (folder location) I should create my HTML Helper class. Best practice.
For example:
VisualStudioSolution
Controlles
Html
HtmlHelperClass.vb
Models
Views
use this.To use the "#helper" feature in Razor you need to place the CSHTML file in the App_Code folder of your app. There is no "Views/Helpers" folder in ASP.NET MVC 3. ScottGu's blog post was written before the feature was fully implemented, and some of the notes there are not entirely accurate anymore.
To call the "#helper" that you wrote you have to include both the filename as well as the name of the helper inside it. For example, if you have this helper:
~/App_Code/MyHelper.cshtml
And this content:
#helper ShowStuff(string stuff) {
<p>#stuff</p>
}
Then you call it like so:
#MyHelper.ShowStuff("some stuff!")
You have a good structure.
I would change the Html folder with a utility folder.
You can add all kinda helpers there.
Controllers
Models
Views
Utility
Framework (this may be usefull for the bootstrapping of your app)
And there actually no fix "best practice". Just make sure you can find your classes in the obvious places. If not remodel.

Does anyone use ASP.net (webforms) to dynamically generate javascript and/or css files?

I prefer the use of external css and javascript files. There are however many cases where the content of a javascript or css file needs to be dynamic. I'll usually just transfer the javascript or css to inline or inpage code in my aspx page and handle the dynamic stuff there.
Does anyone have a better approach? Would there be a way to generate entire js or css files using asp.net's regular templating language?
I'm currently using webforms but I'd be interested in solving this issue in MVC as well.
Thanks
I've used a HTTPHandler to send back dynamic javascript before. But not something that inherits from System.Web.UI.Page.
Using a HTTPHandler and an ASHX or AXD is the "ASP.Net" way to send back resources dynamically.
I have used handlers for dynamic css. Depending on what you need, you can do the same for js files.
I had a css file with placeholders for the pieces that needed to be dynamic like ##bacgroundcolor##, and the handler just replaced as appropriate.
I have also used an approach where I use css classes to mark html elements that need special behaviors. Then the static js, looks for this elements and hook the appropriate handlers. This is something that certainly would be even easier with jquery (I did it with regular js back then :().
I've done this in an aspx page before, but in my opinion the WebForm style doesn't suit itself well to rendering strictly javascript or CSS. Every time I've done it, the page has ended up looking quite a bit like classic ASP.
hopefully the actual JavaScript you are using would stay static and you would just pass parameters to the JavaScript methods.
I have taken JavaScript code that had been in the markup of a page and containing things like <%= control.ClientID %> and replaced it with static JavaScript. I refactored the code into a class, I then refactored these variable parts into class members. The page creates an instance of the class, with things like ClientID set. The functions can then be static.

Resources