我有一个网站,我最近添加了文件附件功能;然而,剑道.SaveUrl()
似乎没有"看到"控制器!我已验证所有文件路径都是正确的。我在默认映射语句之前添加了自定义地图...浏览器不是问题,我已经尝试过IE,Chrome,FireFox和Safari,同样的错误。请帮忙!
错误:
System.Web.HttpException (0x80004005): The controller for path '/Shared/RepairAttachment' was not found or does not implement IController.
Global.asax.cs中的地图:
routes.MapRoute(
name: "/Shared/RepairAttachment",
url: "Controllers/RepairAttachmentsController/{Save}/{IEnumerable<HttpPostedFileBase>}",
defaults: new { controller = "RepairAttachmentsController", action = "Save", attachments = UrlParameter.Optional });
文件上传代码:
@(Html.Kendo().Upload()
.ShowFileList(true)
.Async(async => async
.SaveUrl("SharedRepairAttachment")
)
.Name("frmUploadController")
.Multiple(true)
.Events(events => events
.Complete("onRepairAttachmentUploadComplete")
.Error("onRepairAttachmentUploadError")
)
)
控制器(部分):
public class RepairAttachmentsController : Controller
{
[HttpPost]
[HttpOptions]
public ActionResult Save(IEnumerable<HttpPostedFileBase> attachments)
{
try
{
HarbisonFischerDBEntities db = new HarbisonFischerDBEntities();
foreach (var file in attachments)
{
Repair r = db.Repairs.Find(ViewBag.RepairID);
// Each Customer and repair gets their own folder
string srvrpath = Path.Combine(ConfigurationManager.AppSettings["ServerPathForFiles"].ToString(), r.CustomerLocation.Customer.OracleName);
srvrpath = Path.Combine(srvrpath, r.RepairID.ToString());
if (Directory.Exists(srvrpath) == false) { Directory.CreateDirectory(srvrpath); }
string fileName = Path.GetFileName(file.FileName); // Some browsers send file names with full path; we only care about the file name.
string destinationPath = Path.Combine(Server.MapPath(srvrpath), fileName);
file.SaveAs(destinationPath);
RepairAttachment ra = new RepairAttachment();
ra.FileName = file.FileName;
ra.Title = Path.GetFileNameWithoutExtension(file.FileName);
ra.RepairID = ViewBag.RepairID;
db.RepairAttachments.Add(ra);
db.SaveChanges();
}
}
catch (Exception ex)
{
Logic.ExceptionLogging.LogException("RepairAttachmentsController", ex, null, null);
return Json(new { status = "Upload Failed: " + ex.Message }, "text/plain");
}
return Json(new { status = "Upload succeeded." }, "text/plain");
}
答案 0 :(得分:0)
我不认识剑道,但我很难想到这会奏效:
routes.MapRoute(
name: "/Shared/RepairAttachment",
url: "Controllers/RepairAttachmentsController/{Save}/{IEnumerable<HttpPostedFileBase>}",
defaults: new { controller = "RepairAttachmentsController", action = "Save", attachments = UrlParameter.Optional });
如果您要向&#34; / Shared / RepairAttachment&#34;发送请求,则url
参数中的模式应与此匹配。所以,我希望你需要像
routes.MapRoute(
name: "RepairAttachment",
url: "/Shared/RepairAttachment",
defaults: new { controller = "RepairAttachments", action = "Save", attachments = UrlParameter.Optional });
请注意我
url
参数的值以匹配传入的请求IEnumerable<HttpPostedFileBase> attachments
参数。答案 1 :(得分:0)
删除&#34;控制器&#34;来自您路线的controller
参数。通过&#34;控制器&#34;在路由名称中,MVC管道实际上正在寻找一个名为RepairAttachmentsControllerController
的控制器类,它不存在。
您的网址定义可能也会导致问题。在不知道您的确切设置的情况下,并根据错误消息,路径定义应该看起来像这样:
routes.MapRoute(
name: "/Shared/RepairAttachment",
url: "Shared/RepairAttachment",
defaults: new { controller = "RepairAttachments", action = "Save"});