【问题标题】:"Server Error in '/' Application" When uploading an image“'/'应用程序中的服务器错误”上传图像时
【发布时间】:2017-04-27 14:12:36
【问题描述】:

我想上传个人资料图片到我的系统。但我点击上传,“'/'应用程序中的服务器错误”消息出现。我还发现 URL 与正确的 URL 有点不同应该是这样的。
/ProfileController/UploadPhoto
但是这里的网址是/admin/ProfileController/UploadPhoto
应该怎么做才能完成这项工作?
这是我在控制器中的代码

[HttpPost]
public ActionResult UploadPhoto(HttpPostedFileBase file)
{
     if (file != null && file.ContentLength > 0)
     {
            var user = Session["userID"].ToString();
            var fileExt = Path.GetExtension(file.FileName);
            var fileName = user + ".png";

            if (fileExt.ToLower().EndsWith(".png") || fileExt.ToLower().EndsWith(".jpg"))
            {
                 var filePath = HostingEnvironment.MapPath("~/Content/images/profile/") + fileName;
                var directory = new DirectoryInfo(HostingEnvironment.MapPath("~/Content/images/profile/"));

                if (directory.Exists == false)
                {
                     directory.Create();
                }
                ViewBag.FilePath = filePath.ToString();
                file.SaveAs(filePath);
                return RedirectToAction("Index", new { Message = ManageMessageId.PhotoUploadSuccess });

            }
            else
            {
                  return RedirectToAction("Index", new { Message = ManageMessageId.FileExtensionError });

            }
     }
     return RedirectToAction("Index", new { Message = ManageMessageId.Error }); 
}             

这是视图中的代码

<dl class="dl-horizontal">
        <dd>
            @if (User != null)
            {
                 var imgUrl = Url.Content("Content/Images/" + User + ".png") + "?time=" + DateTime.Now.ToString();
                <div class="input-field col s12">
                    <div class="input-field col s12">
                        <img src="@imgUrl" height="250" width="250" />
                    </div>
                    <div class="mngimg">
                        @using (Html.BeginForm("UploadPhoto", "ProfileController", FormMethod.Post, new { enctype = "multipart/form-data" }))
                        {
                            <div class="input-field col s12">
                                <input type="file" name="file" id="files" onchange="this.form.submit()" />
                            </div>
                        }
                    </div>
                </div>
            }
        </dd>
    </dl>

RouteConfig.cs

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "AdminPanel",
            "admin/{controller}/{action}/{id}",
            new {`controller="Home", action = "Index", id = UrlParameter.Optional },
            new[] { "OnlineElection.Controllers.Admin" } );

routes.MapRoute(
            name:"Default",
            url:"{controller}/{action}/{id}",
            defaults:new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces:new[] { "OnlineElection.Controllers" }
        );

【问题讨论】:

  • 您是否使用 RouteArea 属性修饰了您的 Controller 类以创建 URL 的“/admin/”部分?
  • @Colinm,不,我没有做过类似的事情。这就是为什么我无法弄清楚它是如何发生的
  • 请更新您的问题以包含确切的错误消息,因为“'/' 应用程序中的服务器错误”可能意味着任何事情。
  • 你是如何映射你的路线的?
  • 您的using Html.BeginForm 路由参数错误,ProfileController 应该只是Profile。请熟悉 MVC 路由并使用 App_Code 文件夹中 Routes.cs 文件中的相关路由信息更新问题(假设您不在 ASP.NET Core 上)msdn.microsoft.com/en-us/library/cc668201.aspx

标签: c# asp.net-mvc photo-upload


【解决方案1】:

您发布到的表单 URL 无效。

在使用控制器时,您可以省略 Controller 单词并仅使用控制器的名称,在这种情况下,ProfileController 变为 Profile,这也适用于 URL。

您的Html.BeginForm 实际上应该是 @using (Html.BeginForm("UploadPhoto", "Profile", FormMethod.Post, new { enctype = "multipart/form-data" }))

【讨论】:

  • 如果您不介意我可以请一点帮助吗?事实上,你是这方面的最佳人选。我在同一个问题中遇到了另一个例外。如果可以请看一下。它工作正常,但后来开始创建异常。如果你能做到,对我帮助很大。请检查链接。 stackoverflow.com/questions/41120975/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-29
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多