English 中文(简体)
When should I use HtmlHelper Extension Methods?
原标题:

I am increasingly finding situations where my ASP.NET MVC view requires some logic to perform layout. These routines have no place being in either my model or my controller. I have 3 options:

  1. Write lots of <% %> inline in the view.
  2. Write less <% %> in a number of partial views.
  3. Write an HtmlHelper Extension method.

It is the last option that confuses me. Is it OK to do this if the logic is specific to only that one view? The extension would be visible to the Html object of every other view, and it will never be needed.

Any suggestions?

最佳回答

I personally prefer option 3 ("Write an HtmlHelper Extension method") because those bodies of code lend themselves to be easily unit testable.

I really do wish extension methods could be placed on internal or nested classes because you are right, you will begin to pollute your namespaces with tons of extension methods which are only used in one View.

I d recommend sequestering these HtmlHelper extension methods in static classes in a custom namespace per View that you manually reference in the View so as to limit the number of extension methods available throughout your project.

问题回答

I would, generally, limit both partial views and extension methods to reusable components, but am not pedantic about it. If you feel that either one improves the readability of your code, then go ahead and use them. You might want to consider a separate namespace/helper class for helper methods that are only used by one set of views -- sort of like segregating your partials per controller.

You might also want to consider using more whitespace (even though it s the silent killer) to improve readability. I ve implemented output compression in my base controller to limit the impact of whitespace on download time.

Is it OK to do this if the logic is specific to only that one view?

Yes. Read on...

The extension would be visible to the Html object of every other view, and it will never be needed.

No true. It depende how you register the extension method for the view. This is only the case if you add the namespace to the web.config namespaces section.

If you want to use the extension method on a single view just import its namespace to the single view:

<%@ Import Namespace="NamespaceOf.Your.ExtensionMethods.ForThisViewOnly"%>




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签