C# 使用远程CSV并转换为模型以便在MVC中查看?
我在远程服务器上有一个CSV文件,可以通过URL访问,例如:。它只是一个键/值对的记录。在我的控制器中,如何使用该CSV文件并将其作为模型传递给视图?我对MVC3有点陌生,所以我非常感谢您的帮助 以下是我的CSV示例:C# 使用远程CSV并转换为模型以便在MVC中查看?,c#,asp.net,asp.net-mvc,asp.net-mvc-3,csv,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Csv,我在远程服务器上有一个CSV文件,可以通过URL访问,例如:。它只是一个键/值对的记录。在我的控制器中,如何使用该CSV文件并将其作为模型传递给视图?我对MVC3有点陌生,所以我非常感谢您的帮助 以下是我的CSV示例: key,value Key1,ValueA Key2,ValueB Key3,ValueC Key4,ValueD 我不会从控制员那里打电话。我将使用接口驱动的开发将该逻辑委托给服务 为CSV解析器生成大量结果。因此,只需构造一个HTTP请求,解析CSV,然后将其映射到View
key,value
Key1,ValueA
Key2,ValueB
Key3,ValueC
Key4,ValueD
我不会从控制员那里打电话。我将使用接口驱动的开发将该逻辑委托给服务 为CSV解析器生成大量结果。因此,只需构造一个HTTP请求,解析CSV,然后将其映射到ViewModel 因此,您的控制器可以如下所示:
private ICsvParsingService _csvParsingService; // tip: use DI to inject the concrete in ctor.
[HttpGet]
public ActionResult Csv()
{
var csv = _csvParsingService.Parse("http://mydomain.com/test.csv");
var model = Mapper.Map<SomeCsvType,YourModel>(csv); // AutoMapper. Or you could do L-R.
return View(model);
}
private ICsvParsingService\u csvParsingService;//提示:使用DI将混凝土注入混凝土。
[HttpGet]
公共行动结果Csv()
{
var csv=_csvParsingService.Parse(“http://mydomain.com/test.csv");
var model=Mapper.Map(csv);//AutoMapper。或者您可以执行L-R。
返回视图(模型);
}
这样,如果您决定使用不同的CSV解析器(或使用自己的解析器),您的控制器就不需要更改。您可以在整个应用程序中重复使用此服务。我不会从控制器调用它。我将使用接口驱动的开发将该逻辑委托给服务 为CSV解析器生成大量结果。因此,只需构造一个HTTP请求,解析CSV,然后将其映射到ViewModel 因此,您的控制器可以如下所示:
private ICsvParsingService _csvParsingService; // tip: use DI to inject the concrete in ctor.
[HttpGet]
public ActionResult Csv()
{
var csv = _csvParsingService.Parse("http://mydomain.com/test.csv");
var model = Mapper.Map<SomeCsvType,YourModel>(csv); // AutoMapper. Or you could do L-R.
return View(model);
}
private ICsvParsingService\u csvParsingService;//提示:使用DI将混凝土注入混凝土。
[HttpGet]
公共行动结果Csv()
{
var csv=_csvParsingService.Parse(“http://mydomain.com/test.csv");
var model=Mapper.Map(csv);//AutoMapper。或者您可以执行L-R。
返回视图(模型);
}
这样,如果您决定使用不同的CSV解析器(或使用自己的解析器),您的控制器就不需要更改。您可以在整个应用程序中重复使用此服务。这似乎是一个基本问题。像这样的事情应该让你开始
WebClient client = new WebClient();
string csvContents = client.DownloadString(UrlAsString);
string[] csvLines = csvContents.Split(new string[] {"\n", "\r\n"},
StringSplitOptions.RemoveEmptyEntries);
SomeModel model = new SomeModel()
model.KeyValuePairs = csvLines.Select(x => x.Contains(","))
.Select(x => new KeyValuePair(x.Split(",")[0],
x.Split(",")[1]);
public class SomeModel()
{
public IEnumerable<KeyValuePair> KeyValuePairs { get; set; }
}
public class KeyValuePair()
{
public KeyValuePair() { }
public KeyValuePair(string Key, string Value)
{
this.Key = Key;
this.Value = Value;
}
public string Key { get; set; }
public string Value { get; set; }
}
WebClient=newWebClient();
string csvContents=client.DownloadString(UrlAsString);
字符串[]csvLines=csvContents.Split(新字符串[]{“\n”,“\r\n”},
StringSplitOptions.RemoveEmptyEntries);
SomeModel模型=新的SomeModel()
model.KeyValuePairs=csvLines.Select(x=>x.Contains(“,”))
.选择(x=>newkeyvaluepair(x.Split(“,”)[0],
x、 拆分(“,”[1]);
公共类模型()
{
public IEnumerable KeyValuePairs{get;set;}
}
公共类KeyValuePair()
{
公钥值对(){}
公钥值对(字符串键、字符串值)
{
这个。键=键;
这个。值=值;
}
公共字符串密钥{get;set;}
公共字符串值{get;set;}
}
这似乎是一个基本的问题。这样的问题应该让你开始
WebClient client = new WebClient();
string csvContents = client.DownloadString(UrlAsString);
string[] csvLines = csvContents.Split(new string[] {"\n", "\r\n"},
StringSplitOptions.RemoveEmptyEntries);
SomeModel model = new SomeModel()
model.KeyValuePairs = csvLines.Select(x => x.Contains(","))
.Select(x => new KeyValuePair(x.Split(",")[0],
x.Split(",")[1]);
public class SomeModel()
{
public IEnumerable<KeyValuePair> KeyValuePairs { get; set; }
}
public class KeyValuePair()
{
public KeyValuePair() { }
public KeyValuePair(string Key, string Value)
{
this.Key = Key;
this.Value = Value;
}
public string Key { get; set; }
public string Value { get; set; }
}
WebClient=newWebClient();
string csvContents=client.DownloadString(UrlAsString);
字符串[]csvLines=csvContents.Split(新字符串[]{“\n”,“\r\n”},
StringSplitOptions.RemoveEmptyEntries);
SomeModel模型=新的SomeModel()
model.KeyValuePairs=csvLines.Select(x=>x.Contains(“,”))
.选择(x=>newkeyvaluepair(x.Split(“,”)[0],
x、 拆分(“,”[1]);
公共类模型()
{
public IEnumerable KeyValuePairs{get;set;}
}
公共类KeyValuePair()
{
公钥值对(){}
公钥值对(字符串键、字符串值)
{
这个。键=键;
这个。值=值;
}
公共字符串密钥{get;set;}
公共字符串值{get;set;}
}
哪部分需要帮助?下载文件?解析CSV?将其用作模型?是否考虑在每个请求中解析CSV?可以考虑缓存策略。哪部分需要帮助?下载文件?解析CSV?将其用作模型?是否考虑在每个请求中解析CSV?可以考虑缓存策略。