views:

708

answers:

3

I'm using the latest release of resharper. Just downloaded and installed last week so I'm new to using it. The problem I'm having is that in my ASP.NET MVC application all of my controllers say they are never used and all the methods within the controller say they are never used.

Is there a way to suppress the messages or for resharper to understand they are used?

+9  A: 

You can turn on/off the various warnings and hints that Resharper gives you.

HOWEVER, if you write unit tests then they WILL be used and Resharper will be happy.

update

You could consider telling Resharper to ignore the unused members of your controller by doing this at the top and bottom of your controller class...

public class MyController: Controller
{
    // ReSharper disable UnusedMember.Global

    ...        

    // ReSharper restore UnusedMember.Global
}

... or, at least put these comments around the public methods on your controller (I use StyleCop and so all the public methods will be grouped together anyway. This is reasonably unobtrusive, and would get rid of this warning from Resharper.

Martin Peck
Unit tests, huh? What are those? :)
Mike Roosa
Naughty boy. Write 1000 times "I must write unit tests" ;)
Martin Peck
I do want warnings when things aren't used, but the controllers are used. What I'm really asking is there any way for resharper to know this.
Mike Roosa
The problem is that these methods are used via reflection. There's no way for Resharper to know whether they'll actually get called in real life (it can't tell that your routes are wrong). Are you looking for a way to say "stop looking at public methods on a Controller because you'll never get it right" or something that can tell that your routes would result in an action being used?
Martin Peck
Yes, I'm afraid when I refactor one day and resharper says a certain method isn't used that I'm going to remove it. I know unit tests will help me there, but just because a unit test uses the method doesn't mean the app does.
Mike Roosa
+1  A: 

There's no way I know of that will force Resharper to "know" that a method is used (I've never experienced this problem with Resharper).

If you don't want to globally turn on/off various inspection options, you can choose to suppress specific inspection messages with a comment. Like so...

// ReSharper disable UnusedPrivateMember
private void NotUsed ()
{
    // ... Code ...
}
// ReSharper restore UnusedPrivateMember
Steve Dignan
+3  A: 

Yep, current ReSharper version doesn't place references on MVC controllers (although, next one will).

In meantime you can mark controller class and action methods with ImplicitUse attribute. You can find it in JetBrains.Annotations assembly. If you don't want external dependency, you can add necessary annotations attributes to your project, you will find them in ReSharper->Options->Code Inspection->Code Annotation->Copy default implementation.

derigel
This is really the best solution - thanks!
Mike Scott