我正在寻找一个WebApi示例,其中默认路由将给定调用者返回给定的html页面.我的路线和行动设置如下.我只想向他发送index.html页面,而不是重定向,因为他在正确的位置.
http://localhost/Site // load index.html
// WebApiConfig.cs
config.Routes.MapHttpRoute(
name: "Root",
routeTemplate: "",
defaults: new { controller = "Request", action = "Index" }
);
// RequestControlller.cs
[HttpGet]
[ActionName("Index")]
public HttpResponseMessage Index()
{
return Request.CreateResponse(HttpStatusCode.OK, "serve up index.html");
}
Run Code Online (Sandbox Code Playgroud)
如果我使用这个错误,那么更好的方法是什么,你能指点我一个例子吗?
WebApi 2与.NET 4.52
编辑:嗯,改进了它,但得到了json头而不是页面内容.
public HttpResponseMessage Index()
{
var path = HttpContext.Current.Server.MapPath("~/index.html");
var content = new StringContent(File.ReadAllText(path), Encoding.UTF8, "text/html");
return Request.CreateResponse(HttpStatusCode.OK, content);
}
{"Headers":[{"Key":"Content-Type","Value":["text/html; charset=utf-8"]}]}
Run Code Online (Sandbox Code Playgroud) 我们的环境目前有
.NET Framework 2.0 50727
.NET Framework 3.0 30729
.NET Framework 3.5 30729
.NET Framework 4.5.2 379893
Run Code Online (Sandbox Code Playgroud)
本周推出4.7.针对4.6.x的应用程序是使用4.7,还是我还需要安装4.6?
使用 CsvHelper 6.1.0 写入 CSV 文件时,文档仅一次提到在写入时控制标题名称:
“写作时,只使用名字。”
我的代码符合给出的示例:
Map(m => m.ReportingGroup).Name("Reporting Group");
Map(m => m.Merchant);
Map(m => m.ActivityDate).Name("Activity Date");
Map(m => m.SettlementDate).Name("Settlement Date");
Run Code Online (Sandbox Code Playgroud)
写操作期间不写入空格!
报告组、商户、活动日期、结算日期
如果需要,我的 Read() 和 Write():
public static IEnumerable<TR> Read<TR, TMap>(string filename)
where TMap : ClassMap
{
List<TR> records;
using (var sr = new StreamReader(filename))
{
var csv = new CsvReader(sr);
csv.Configuration.RegisterClassMap<TMap>();
records = csv.GetRecords<TR>().ToList();
}
return records;
}
public static void Write<T>(IEnumerable<T> records, string filename, Logger logger)
{
var enumerable = records as IList<T> ?? records.ToList(); …Run Code Online (Sandbox Code Playgroud) 我正在处理不同CSV文件的管道,因此想要编写通用的读写器,只需传递必要的类型.RegisterClassMap()通常采用您定义的Mapper.第一个例子是不使用泛型的工作代码.
public static IEnumerable<CacheEntryRequest> ReadCacheRequestFile(string filename)
{
List<CacheEntryRequest> requests;
using (var sr = new StreamReader(filename))
{
var csv = new CsvReader(sr);
csv.Configuration.RegisterClassMap<CacheEntryRequestMapper>();
requests = csv.GetRecords<CacheEntryRequest>().ToList();
}
return requests;
}
Run Code Online (Sandbox Code Playgroud)
接下来是我没有尝试使用Mapper类型的泛型参数编写泛型方法:
public static IEnumerable<TR> Read<TR, TMap>(string filename)
{
List<TR> records;
using (var sr = new StreamReader(filename))
{
var csv = new CsvReader(sr);
csv.Configuration.RegisterClassMap<TMap>();
records = csv.GetRecords<TR>().ToList();
}
return records;
}
Run Code Online (Sandbox Code Playgroud)
之后RegisterClassMap抱怨:
类型'TMap'不能用作泛型类型或方法'CsvConfiguration.RegisterClassMap()'中的类型参数'TMap'.从'TMap'到'CsvHelper.Configuration.CsvClassMap'没有装箱或拆箱转换或类型参数转换.
所需的用法是:
var records = CsvFile.Read<CacheEntryRequest, CacheEntryRequestMapper>(filename);
Run Code Online (Sandbox Code Playgroud) 如果CSV文件中缺少某个字段,则会引发异常.当字段丢失时,我宁愿映射另一个值(例如空字符串).
Map(dest => dest.PatientID).Name("Patient ID");
CsvHelper.CsvMissingFieldException: 'Fields 'Patient ID' do not exist in the CSV file.'
Run Code Online (Sandbox Code Playgroud)
如果使用配置设置IgnoreReadingExceptions,则不会将任何记录读入结果.
var csv = new CsvReader(sr);
csv.Configuration.RegisterClassMap<TMap>();
csv.Configuration.IgnoreReadingExceptions = true;
records = csv.GetRecords<TR>().ToList();
Run Code Online (Sandbox Code Playgroud)
如何更改映射,以便在映射字段为MISSING时,可以将其替换为另一个表达式?
如果有,那么Find-Or-Create的C#成语是什么?伪代码:
private IEntity FindOrCreateEntity(int id, object properties)
{
Entity foundEntity = _db.Find(id);
if (foundEntity == null)
{
foundEntity = _db.Create<Entity>(properties);
}
return foundEntity;
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢以相同的方法查找和创建存在.