With my controller I am able to upload files to certain path. I am trying to figure out how to delete iterated file in my view.
Controllers method:
[Authorize(Roles = "Moderatorzy")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DeleteFile(string file)
{
if (!System.IO.File.Exists(file))
{
return NotFound();
}
System.IO.File.Delete(file);
return View("Edit");
}
View file:
<form asp-action="Edit" method="post" enctype="multipart/form-data">
<input type="hidden" asp-for="ID" />
(...)
@if (Enumerable.Count(ViewBag.fileList) != 0)
{
<dir>Files to download:</dir>
{
foreach (var file in ViewBag.fileList)
{
<a class="down" href="Autobus/DeleteFile?file=@(ViewBag.fileDirectory + file)"><dir>@file<span>;</span></dir></a>
}
}
}
(...)
<div class="text-center">
<button class="btn btn-success" type="submit">Zapisz</button>
<a href="javascript:history.go(-1)" class="btn btn-primary">Powrót</a>
</div>
<div class="space"></div>
Right now I am having 2 issues:
1) Autobus
is the controller name. href="Autobus/DeleteFile?file=@(ViewBag.fileDirectory + file)"
gives me path: /Autobus/Autobus/DeleteFile(...)
instead of /Autobus/DeleteFile(...)
. Why?
2) After typig manually just one Autobus
it does not call DeleteFile
method. Why?
Complete generated route path is: http://localhost:50686/Autobus/Autobus/DeleteFile?file=C:\Users\asus\Desktop\Praca%20IT\Programowanie\Projekty\DluzynaSzkola\ASP.NET%20Core%20-%20ostatni\Dluzyna_Szkola_2\BasicConfig\wwwroot/uploaded/bus/1.jpg
P.S. I am guessing it might be something wrong with routing.
My final working solution:
View file:
Also in my DeleteFile method I had to add ViewBags: