Я хочу загрузить изображение профиля в свою систему. Но я нажимаю кнопку «Загрузить», «Ошибка сервера в '/' приложении» появляется сообщение. Также я выяснил, что URL-адрес немного отличается от правильного URL-адреса, который должен быть таким.
/ProfileController/UploadPhoto
Но URL в здесь /администратор/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' атрибут для создания«/ администратора /»часть URL? – ColinM
@Colinm, Нет, я не сделал ничего подобного. Вот почему я не могу понять, как это происходит. – Mike
Пожалуйста, обновите свой вопрос, чтобы включить точное сообщение об ошибке, поскольку «Ошибка сервера в приложении /» может означать что угодно. – ColinM