Skip to content

Instantly share code, notes, and snippets.

@nikanos
Last active November 29, 2022 17:14
Show Gist options
  • Save nikanos/8cf76a3cf268f0d1f0558b1a762274a1 to your computer and use it in GitHub Desktop.
Save nikanos/8cf76a3cf268f0d1f0558b1a762274a1 to your computer and use it in GitHub Desktop.
ASP.NET MVC 4 Print server variables snippet
@model Demo.Asp.PrintServerVars.Models.ServerVarsViewModel
@{
ViewBag.Title = "Home Page";
}
<div class="row">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Key</th>
<th scope="col">Value</th>
</tr>
</thead>
<tbody>
@if (Model.ServerVariables != null)
{
for (int i = 0; i < Model.ServerVariables.Count; i++)
{
<tr>
<th scope="row">@(i+1)</th>
<td>@Model.ServerVariables[i].Key</td>
<td>@Model.ServerVariables[i].Value</td>
</tr>
}
}
</tbody>
</table>
</div>
public class ServerVar
{
public string Key { get; set; }
public string Value { get; set; }
}
public class ServerVarsViewModel
{
public IList<ServerVar> ServerVariables { get; set; }
}
public static class Config
{
public static IEnumerable<string> SkipVarList
{
get
{
var configValue = System.Configuration.ConfigurationManager.AppSettings["SkipVarList"];
return configValue.Split(',');
}
}
}
public class HomeController : Controller
{
[OutputCache(NoStore = true, Duration = 0)]
public ActionResult Index()
{
var model = new ServerVarsViewModel();
var serverVariables = new List<ServerVar>();
foreach (string serverVarKey in Request.ServerVariables.AllKeys)
{
if (Common.Config.SkipVarList.Any(x => x == serverVarKey))
continue;
serverVariables.Add(new ServerVar { Key = serverVarKey, Value = Request.ServerVariables[serverVarKey] });
}
model.ServerVariables = serverVariables;
return View(model);
}
}
<configuration>
<appSettings>
...
<add key="SkipVarList" value="PATH_TRANSLATED,LOCAL_ADDR,SERVER_NAME,SERVER_PORT,SERVER_SOFTWARE,APPL_PHYSICAL_PATH"/> <!-- Comma seperated -->
...
</appSettings>
...
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment