public class MimeController : Controller { public IActionResult Index() { using (var rs = WebRequest.Create("http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types") .GetResponse().GetResponseStream()) { if (rs == null) return null; var sr = new StreamReader(rs, Encoding.UTF8); var content = sr.ReadToEnd(); var arr = content.Split("\n"); var sb = new StringBuilder(); foreach (var s in arr) { if (s.StartsWith("#")) continue; var kv = s.Split("\t").Where(c => !string.IsNullOrEmpty(c)).ToArray(); if (kv.Length == 2) sb.AppendLine($"\"{kv[1]}\",\"{kv[0]}\""); } sr.Close(); return Content(sb.ToString()); } } }
结果:
多谢
1\搜索 C# 读文件 readline
2. 循环 读取每一行
3. 判断是否有 #好
4,截取字符串,找到自己想要的字符。