Where should I locate shared @helper functions in

2019-03-12 02:28发布

问题:

I have a helper function that turns minutes into hours/mins. I currently have it in my layout.cshtml but each page cannot see the function. Where should I put the helper function such that every page can see it?

@helper DisplayElapsedTime(int timeInMins){
    String timeStr = "";
    if (timeInMins >= 60) {
        int hours = timeInMins/60;
        timeInMins -= hours * 60;
        timeStr = hours + "h ";
    }
    if (timeInMins > 0){
        timeStr += timeInMins + "m";
    }
    @timeStr;
}

回答1:

You should put it into the App_Code folder. There is an awesome article for you to read ASP.NET MVC Helpers