MVC 4路由映射不起作用

时间:2014-05-28 20:19:22

标签: c# asp.net-mvc asp.net-mvc-4 routes

我有一个网站,我最近添加了文件附件功能;然而,剑道.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");
    }

2 个答案:

答案 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参数的值以匹配传入的请求
  • 删除&#34;控制器&#34;来自&#34; RepairAttachmentsController&#34;因为除非您的控制器命名为&#34; RepairAttachmentsControllerController&#34;
  • ,否则不需要它
  • 删除了&#34; {IEnumerable ...}&#34;完全是东西。我知道你试图告诉MVC你将发送一组文件,但你不会在这里声明。 MVC's model binding features会自行解决,因为您的操作方法需要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"});