0

I am writing an asp.net web API using Visual Studio 2019. When I write as in the example below, Visual Studio recommends me making the method static. So I followed the suggestions and made all methods of the Web API static. Is this correct? What is the advantage if it is correct and what is the disadvantage if it is wrong? Thank you...

Severity Code Description Project File Line Suppression State Message CA1822 Member AdresleriGetir does not access instance data and can be marked as static (Shared in VisualBasic) Devriye.WebApi**

My method:

[HttpPost]
public static Adres[] AdresleriGetir([FromBody]GirisParametresi girisParametresi)
{
    if (girisParametresi != null)
    {
        string query = @"SELECT * FROM ADRESLER WHERE AKTIF=1";
        Cagri cagri = new Cagri()
        {
            Proje = "Devriye.WebApi",
            Modul = "AdresController",
            Metot = "AdresGetir",
            Nesne = new JavaScriptSerializer().Serialize(girisParametresi)
        };
        Log log = new Log(null, cagri, girisParametresi.Oturum);
        using (DataTable dataTable = DataAccessLayer.VerileriGetir(query, null, log))
        {
            List<Adres> adresler = new List<Adres>();
            if (dataTable.Rows.Count > 0)
            {
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    Adres adres = new Adres();
                    try { adres.Cadde = Convert.ToString(dataTable.Rows[i]["Cadde".ToUpperInvariant()], WebApiConfig.CultureInfo); } catch (Exception ex) { if (log != null) { log.Hata = new Hata() { Aciklama = ex.Message, HataKodu = 997 }; Task.Run(() => DataAccessLayer.LogKaydet(log)); } }
                    try { adres.EklenmeTarihi = Convert.ToDateTime(dataTable.Rows[i]["EklenmeTarihi".ToUpperInvariant()], WebApiConfig.CultureInfo); } catch (Exception ex) { if (log != null) { log.Hata = new Hata() { Aciklama = ex.Message, HataKodu = 997 }; Task.Run(() => DataAccessLayer.LogKaydet(log)); } }
                    try { adres.ID = Convert.ToInt32(dataTable.Rows[i]["ID".ToUpperInvariant()], WebApiConfig.CultureInfo); } catch (Exception ex) { if (log != null) { log.Hata = new Hata() { Aciklama = ex.Message, HataKodu = 997 }; Task.Run(() => DataAccessLayer.LogKaydet(log)); } }
                    try { adres.Il = Convert.ToString(dataTable.Rows[i]["Il".ToUpperInvariant()], WebApiConfig.CultureInfo); } catch (Exception ex) { if (log != null) { log.Hata = new Hata() { Aciklama = ex.Message, HataKodu = 997 }; Task.Run(() => DataAccessLayer.LogKaydet(log)); } }
                    try { adres.Ilce = Convert.ToString(dataTable.Rows[i]["Ilce".ToUpperInvariant()], WebApiConfig.CultureInfo); } catch (Exception ex) { if (log != null) { log.Hata = new Hata() { Aciklama = ex.Message, HataKodu = 997 }; Task.Run(() => DataAccessLayer.LogKaydet(log)); } }
                    try { adres.KapiNo = Convert.ToString(dataTable.Rows[i]["KapiNo".ToUpperInvariant()], WebApiConfig.CultureInfo); } catch (Exception ex) { if (log != null) { log.Hata = new Hata() { Aciklama = ex.Message, HataKodu = 997 }; Task.Run(() => DataAccessLayer.LogKaydet(log)); } }
                    try { adres.Mahalle = Convert.ToString(dataTable.Rows[i]["Mahalle".ToUpperInvariant()], WebApiConfig.CultureInfo); } catch (Exception ex) { if (log != null) { log.Hata = new Hata() { Aciklama = ex.Message, HataKodu = 997 }; Task.Run(() => DataAccessLayer.LogKaydet(log)); } }
                    try { adres.PostaKodu = Convert.ToInt32(dataTable.Rows[i]["PostaKodu".ToUpperInvariant()], WebApiConfig.CultureInfo); } catch (Exception ex) { if (log != null) { log.Hata = new Hata() { Aciklama = ex.Message, HataKodu = 997 }; Task.Run(() => DataAccessLayer.LogKaydet(log)); } }
                    try { adres.Sokak = Convert.ToString(dataTable.Rows[i]["Sokak".ToUpperInvariant()], WebApiConfig.CultureInfo); } catch (Exception ex) { if (log != null) { log.Hata = new Hata() { Aciklama = ex.Message, HataKodu = 997 }; Task.Run(() => DataAccessLayer.LogKaydet(log)); } }
                    adresler.Add(adres);
                }
                return adresler.ToArray();
            }
            else
            {
                return null;
            }
        }
    }
    else
    {
        return null;
    }
}
  • Please share your complete controller code. – Shan Nautiyal Nov 01 '19 at 07:39
  • The Info from Visual Studio is wrong, because there will be an instance of your class. But not in your code. ;) Somewhere in the Framework it will be created and then your method will be called. So I would suggest to revert your changes. But I belive there's more room for improvemnts in your code. Like others wrote share your controller code. – user743414 Nov 01 '19 at 07:58
  • This is completely unrelated to the question, why have so many try-catch blocks if they're all doing the exact same thing? – ColinM Nov 01 '19 at 09:40
  • try-catch blocks are for catching which parameter fall in exception. my issue is not related with your question. @ColinM – Hakan Yilmaz Nov 01 '19 at 09:56

1 Answers1

1

This function doesn't access any instance data or call any instance methods. I've had similar questions about R# wanting to convert this type of function to static. See this SO question for some reasons. From the accepted answer in the link;

It makes me ask myself if the method in question should actually be part of the type or not. Since it doesn't use any instance data, you should at least consider if it could be moved to its own type. Is it an integral part of the type, or is it really a general purpose utility method?

If it does make sense to keep the method on the specific type, there's a potential performance gain as the compiler will emit different code for a static method.

Ryan.Bartsch
  • 3,698
  • 1
  • 26
  • 52
  • 1
    For MVC an action cannot be static. I have a RESTfull API written using WEB API 2.0 that I reviewed with Code Analysis. It stated to make a few of my actions static. When I did, Action Routing ended up calling my first Action no matter what I passed in. So it broke the routing. I found this article by MS on why. https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/aspnet-mvc-controllers-overview-cs#understanding-controller-actions – Thomas A. Johnson Aug 25 '20 at 16:13