我在C#中有一个基本的控制台应用程序,可以将文件从一个位置复制到另一个位置。我试图将其转移到ASP.NET MVC App,但仍在学习存储方法的基本体系结构。我印象中大多数方法都将ActionResults存储在控制器中,但这是我开始感到困惑的地方。
我的问题:我将c#方法/变量(控制器或模型)放在哪里,什么是调用该方法的最佳方法?我目前正在通过视图对其进行调用。
笔记:
我在控制器中显示了copyFiles方法的错误,指出“并非所有代码路径都返回值”。这不会显示在我的控制台版本中。我想知道是否需要在DisplayCustomer ActionResult下移动它。
我的DisplayCustomer视图在@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
到a方法。我建议看一本基本且有用的教程,其中包含有关使用MVC 5的Entity Framework 6 Code First入门的演示项目。希望这可以帮助...Web Project
MVC Application
Console Application
MVC Application
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句