Like the title said, In what folder should I put my extension methods in ASP.Net MVC?
What is the best practice for this?
To define and call the extension methodDefine a static class to contain the extension method. The class must be visible to client code. For more information about accessibility rules, see Access Modifiers. Implement the extension method as a static method with at least the same visibility as the containing class.
Perhaps the best example of extension methods are HtmlHelper extensions used in ASP.NET MVC. Extension methods are static methods of static class and they use "this" keyword in argument list to specify the type they extend. The following demo shows how to build extension method that returns word count in string.
You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.
Content. The Content folder contains static files like CSS files, images, and icons files. MVC 5 application includes bootstrap.
I don't believe that there is a standard best practice to follow, but I usually do one of two things:
ProjectName.Extensions/System/StringExtensions.cs
ProjectName.Extensions/System.Web/HttpContextExtensions.cs
ProjectName.Extensions/System.Xml/XmlWriterExtensions.cs
And so on...
Regardless of which approach I take, in both cases I'll name the class ClassNameExtensions.cs
(e.g., StringExtensions.cs, ListExtensions.cs, etc.).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With