This project is read-only.

annotation to exclude action or controller

Mar 24, 2014 at 11:59 AM
I ran into a situation where I would like t4 to not generate code for a specific method

it should be possible to do something like this

[T4MVC.Exclude]
public actionresult Iamnotgenerated()
{
...
}

or

[T4MVC.Exclude]
public notgeneratedController : controller
{
...
}

to exclude a method/controller from being generated.
Mar 24, 2014 at 5:02 PM
This is not supported to day, but conceivably could be added if someone wants to drive it. What's an example scenario that calls for it?
Mar 25, 2014 at 1:19 PM
having a static method that redirects the user to a specific actionresult with a string as an error message to show some content along with an error box explaining what went wrong
Mar 25, 2014 at 1:46 PM
Not quite sure I understand the scenario. How is the t4mvc generation harmful there?
Mar 26, 2014 at 2:58 PM
It makes the code uncompilable after you do "run custom tool" as t4mvc applies the virtual keyword to the methods it catches (not allowed on static methods) if you remove it you still have to remove the method in the generated code for "ControllerName.generated.cs" currently in my project I have 1 static method so to me it's just a minor annoyance but to other projects that might use this could have more static methods and this will create of burden of manual editing each time you run the tool.

Not to mention it's rather convenient and you have a fail safe(if uncaught complications/bugs should arise) if you implement an annotation to exclude methods you for some reason don't want to be generated.
Mar 26, 2014 at 7:54 PM
Ok, I'm sold that it would be a good idea. Do you want to try to implement and send a Pull Request?

The way I'd do this is:
  • Define the attributes in T4MVCExtensions
  • Test for them in t4mvc.tt: in IsController to ignore controllers, and in ProcessControllerActionMethods for actions (next to the NonActionAttribute check).
Mar 27, 2014 at 1:27 PM
I'll have the time to try time next week :)
Apr 11, 2014 at 11:42 PM
Wouldn't this be solved if we just exclude static controller methods from T4MVC? In that case, there wouldn't be a need for a separate attribute.
Apr 30, 2014 at 9:01 PM
Yes, if the issue is only about static methods. I didn't realize that T4MVC would try to process those. It should be relatively easy to exclude them.
Nov 11, 2014 at 6:58 PM
I agree that this would be a useful addition.

The scenario my team is facing is that we have several pages that cannot be accessed except by going through variable length processes. As an example, our sign up process is 3-4 forms and so it's difficult/time-consuming for our design team to apply their CSS magic.

As a result we decided to add a new build configuration with a TEST preprocessor directive and within these preproc directives we're defining additional Actions so that we can jump directly to a step (given some hard-coded dummy data).

Unfortunately, we've found that T4MVC is generating code for these regions so when we switch to our Release configuration we get build errors.
Nov 12, 2014 at 12:47 AM
Is it also about static controllers in your case? If so, the proposed fix above would work. I just need someone to send a PR for it :)
Nov 12, 2014 at 2:33 PM
No, in my case it's not static controllers, it's test actions that are included or excluded using preprocessor directives.
Nov 18, 2014 at 2:12 AM
Ok, so sounds like we need both some automatic logic for static case, and some new explicit attributes that can be places on classes and actions.

Any volunteers to take on this work?