ASP.NET MVC 4 custom HTML Helpers folder location - asp.net

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.

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.

Preprocessing ASP.NET MVC Razor views

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

good practice to structure designers files in MVC3

Generally designers design static cshtml files after slicing PSDs before developers can use them to create dynamic pages. How these static html files should be structured in an application, such that:
They have their repository versions
Can share common JQuery/CSS/Images in solution used by other actual coded pages
Doesn't goes to production upon deployments
One good approach is:
Project
|_Area
|_Design
|_Controllers
|_ ModuleA Controller (Has 1 controller with viewname argument and renders it)
|_ ModuleB Controller (Has 1 controller with viewname argument and renders it)
|_Views
|_ModuleA
|_StaticScreen1.cshtml
|_StaticScreen2.cshtml
|_StaticScreen3.cshtml
|_ModuleB
|_StaticScreenA.cshtml
This approach works good for 1st 2 points and is easy for designer too as they just need to keep adding screens in respective view folder. On a downside, this gets deployed to production too. Please suggest a better approach.
Click on each file you don't want and look at the Properties.
Change 'Build Action' to 'None'
When you Publish, the items with Build Action:None will not be included.

ASP.Net MVC: Centralizing CSS and JS dependencies

I'm using jQuery plugins in an ASP.Net MVC site.
I've often to include CSS and JS files as required by the plugins I use in every page. So I want to centralize all those dependencies in a single place in my app. thus if a dependency for a given plug-in is changed or updated, I'll only have to modify a single place in my app.
I've thought in two possible solutions:
Extend the HTMLHelper with a partial method
like GetPlugin("jqgrid"); that
will print out all the script and
style tags needed.
Create a partial view for each
pluginlike jqGridDependencies.ascx
that will contain the script and
style tags needed.
Do you have any other idea? what do you think of both proposals?
Could http://combres.codeplex.com/ provide you with a framework for this.
My only personal objection to this method is that each individual pages will have a unique JavaScript/CSS file where as if you combined and compressed everything into one and simply used classes and events to trigger the JavaScript enhancements as and when needed your site would run a lot faster.

Minify inline javascript during build for ASP.net?

We have a handful of ASP.net pages that include more than 50+ lines of javascript specific to that page. We'd like to minify that javascript during our CruiseControl build process.
We already use the YUI Compressor to compress our full javascript and css files. But we can't figure out how to do the Inline javascript.
Is there an MSBuild task to spin through asp.net pages and minify the javascript?
There is an interesting blog and NuGet package called undleMinifyInlineJsCss to handle this
http://weblogs.asp.net/imranbaloch/archive/2012/07/25/bundling-and-minifying-inline-css-and-js.aspx
I would extract javascript into methods and move them into .js files. and call the functions instead with the relevant parameters from the pages. Not a complicated procedure and much easier to maintain (less code). You can also benefit from client side content caching.
Also: Not sure if it helps but Google's Closure looks really good.
http://code.google.com/closure/
Compression options: http://code.google.com/closure/compiler/docs/api-tutorial3.html
Available as Java executable or web service.
You won't be able to do this without custom coding. Easiest way would probably to create a PreBuild step in the msbuild file which spits through all the .aspx files and regexes all the javascript out. Then use YUI to minify the content and replace the original by the minified version.
You might also want to check MbCompression which compresses alot including your asp.net pages, although I don't believe it also minifies the inline javascript.
It is possible to bundle and minify inline javascript. With templated Razor helpers you could create an extension method like the one below:
public static MvcHtmlString AddScriptSource(this HtmlHelper helper, Func<dynamic, HelperResult> source, string key)
{
string scriptSource = source(null).ToHtmlString();
// Cache scriptSource here
return MvcHtmlString.Empty;
}
Which you would use like this:
#Html.AddScriptSource(#<text>$(document).ready(function() { $('h1').text('The current controller is #ViewContext.RouteData.Values["controller"].ToString()'); });</text>, "test")
I created a bundler and minifier around this a few weeks ago at:
https://github.com/philpeace/CodePeace.StrawberryJam
ASP.NET now has bundling and minification built in as of MVC 4 (it is also available for web forms and web pages as well)
http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

Resources