C Sharp - GetOnly Functions

Normal function

1
2
3
4
5
6
7
8
9
10
11
public string GetIpAddress(Microsoft.AspNetCore.Http.HttpContext context)
{
try
{
return context.Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString();
}
catch (Exception)
{
return null;
}
}

From AFShin, you can also change this to a get only function and use it like a variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
public string GetIpAddress =>
{
try
{
var context = Microsoft.AspNetCore.Http.HttpContext;
return context.Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString();
}
catch (Exception)
{
// I would return a zero ip or broadcast ip: 0.0.0.0 or 255.255.255.255 instead of null
return null;
}
}