What is "Helper" for in Asp.Net MVC?

Asked

Viewed 285 times

6

Please explain to serve Helper on Asp.Net MVC.

I see many examples that contain Helper. I have no idea what it is Helper.

And how to create Helper ?

1 answer

5


Please explain to serve Helper on Asp.Net MVC.

Helper is a static class, outside the group of Controllers, which has replicable logic for the rest of the system.

By "replicable logic", it is any and all logic that is used in two or more places of the system. There are several examples here on the site.

Some examples of Helpers common:

  • Consumption and processing of web services;
  • Creation of Word files, Excel etc.;
  • Generation of links in Views Razor (HtmlHelper, UrlHelper).

And how to create Helper?

Basically, as a common static class, in a namespace proper to it. For example:

namespace MeuProjeto.Helpers
{
    public static class MeuHelper
    {
        public static String MetodoDoHelper1() { ... }
        public static int MetodoDoHelper2() { ... }
        public static void MetodoDoHelper3() { ... }
    }
}

Use:

var retornoString = MeuHelper.MetodoDoHelper1();
var retornoInt = MeuHelper.MetodoDoHelper2();
MeuHelper.MetodoDoHelper3(); // pode não retornar valor, se for o caso.
  • 1

    You’ve helped me so much, thank you !

  • I confess that a question like this was missing even. I hope to have helped :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.