enter image description here public class countryController : Controller //Error :Extension method must be defined in a non-generic static class
-
1You're very unlikely to get any help without some code to debug. Please check out [how to ask](http://stackoverflow.com/help/how-to-ask) in the [help center](http://stackoverflow.com/help) – Brandon Anzaldi Jun 23 '16 at 05:46
-
4The error-message is self-explanatory. *Extension method must be defined in a non-generic static class*! – Salah Akbari Jun 23 '16 at 05:47
-
public ActionResult //only used this kind of methods – Rics Jun 23 '16 at 05:51
-
Need some code snippet to look .Please provide the same – Vicky Jun 23 '16 at 05:54
2 Answers
You can't define an extension method
inside a non-static class countryController
This is not allowed:
public class MyExtensions
{
public static void SomeExtension(this String str)
{
}
}
This is allowed:
public static class MyExtensions
{
public static void SomeExtension(this String str)
{
}
}
You have a method whose first parameter starts with this
, you need to find it and either modify it by removing the this
or move it to a static
helper class.
According to C# Specifications:
10.6.9 Extension methods
When the first parameter of a method includes the
this
modifier, that method is said to be an extension method. Extension methods can only be declared in non-generic, non-nested static classes. The first parameter of an extension method can have no modifiers other than this, and the parameter type cannot be a pointer type.

- 29,485
- 6
- 52
- 63
Add keyword static to class declaration:
// this is a non-generic static class
public static class yourclass
{
}
Following points need to be considered when creating an extension method:
The class which defines an extension method must be non-generic, static and non-nested
Every extension method must be a static method
The first parameter of the extension method should use the this keyword.

- 3,132
- 3
- 20
- 32
-
-
-
-
@user3185569 you are right. instance controller is instantiated once per request, multiple requests can be happening simultaneously. which is not good – MMM Jun 23 '16 at 06:12
-
@Rics The method doesn't have to be `static` to get this error, at least it has to have `this` before the first parameter. example : `public void MyMethod(this string str)` – Zein Makki Jun 23 '16 at 06:18
-