ある場所から別の場所にファイルをコピーするC#の基本的なコンソールアプリケーションがあります。これをASP.NETMVCアプリに転送しようとしていますが、メソッドを保存する場所の基本的なアーキテクチャをまだ学習しています。ほとんどのメソッドがコントローラーにActionResultsとして格納されるという印象を受けていますが、ここで混乱し始めます。
私の質問:c#メソッド/変数(コントローラーまたはモデル)をどこに配置し、メソッドを呼び出すための最良の方法は何ですか?私は現在、ビューを通してそれを求めています。
ノート:
「すべてのコードパスが値を返すわけではない」というcopyFilesメソッドのコントローラー内のエラーを表示します。これは私のコンソールバージョンでは表示されません。これをDisplayCustomerActionResultの下に移動する必要があるかどうか疑問に思います。
私のDisplayCustomerViewは、@ copyFilesの下のifステートメント内に、現在のコンテンツには存在しないというエラーを生成します。これは、使用しているモデルで定義されているため、混乱します。
これが過度に複雑な場合は、お詫び申し上げます。どうもありがとうございました。
**Model:**
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcCustomer.Models
{
public class Customer
{
public string SourceSystem { get; set; }
public string Environment { get; set; }
public void copyFiles(int numberOfFiles)
{
List<string> files = System.IO.Directory.GetFiles(@"C:\Users\acars\Desktop\Test", "*").ToList();
IEnumerable<string> filesToCopy = files.Where(file => file.Contains("Test_File")).Take(10);
foreach (string file in filesToCopy)
{
// here we set the destination string with the file name
string destfile = @"C:\Users\acars\Desktop\RenameFolder\" + System.IO.Path.GetFileName(file);
// now we copy the file to that destination
System.IO.File.Copy(file, destfile, true);
};
}
}
}
**View:**
@model MvcCustomer.Models.Customer
@{
ViewBag.Title = "DisplayCustomer";
}
<h2>Tests to be executed:</h2>
<body>
<div>
The Source System is @Model.SourceSystem <br />
The Environment is @Model.Environment <br />
</div>
@{var SourceSystem=Model.SourceSystem; }
@if (SourceSystem == "TestSourceSystem")
{
@copyFiles(10); //This is where my error is
}
</body>
**Controller:**
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcCustomer.Models;
using MvcCustomer.Controllers;
namespace MvcCustomer.Controllers
{
public class CustomerController : Controller
{
public ActionResult DisplayCustomer()
{
Customer obj = new Customer();
obj.SourceSystem = Request.Form["SourceSystem"];
obj.Environment = Request.Form["Environment"];
return View(obj);
}
public ActionResult copyFiles(int numberOfFiles)
{
List<string> files = System.IO.Directory.GetFiles(@"C:\Users\acars\Desktop\ProductionBEARS", "*").ToList();
IEnumerable<string> filesToCopy = files.Where(file => file.Contains("Test_File")).Take(10);
foreach (string file in filesToCopy)
{
// here we set the destination string with the file name
string destfile = @"C:\Users\acars\Desktop\RenameFolder\" + System.IO.Path.GetFileName(file);
// now we copy the file to that destination
System.IO.File.Copy(file, destfile, true);
};
}
}
}
代わりに移すのConsole Application
にWeb Project
、私はそれを作成した方が良いと思いMVC Application
、最初からやであなたの方法を移動しConsole Application
、新たなにMVC Application
。MVC5を使用したEntityFramework6コードファースト入門に関するデモプロジェクトを含む基本的で便利なチュートリアルの1つをご覧になることをお勧めします。お役に立てれば...
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加