控制器中的 ActionResult 未被调用。设置断点表明 ActionResult fileUpload 没有被命中。
查看和控制器代码如下:(FileUpload.aspx/FileUploadController.cs):
<% using (Html.BeginForm("fileUpload", "FileUpload",
FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<input type="file" name="file" id="file" />
<input type="submit" name="Submit" value="Upload File" />
<span style="color: red; font-weight: bold">
<%=TempData["UploadValidationMessage_Failure"]%>
</span><span style="color: Green; font-weight: bold">
<%=TempData["UploadValidationMessage_Success"]%>
</span>
<%} %>
/// <summary>
/// n/a
/// </summary>
/// <returns>set FileUpload view return value</returns>
public ActionResult Index()
{
return View("FileUpload");
}
/// <summary>
/// Uploads the file and store it in ~\Uploads directory.
/// </summary>
/// <returns></returns>
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult fileUpload(HttpPostedFileBase file)
{
if (file.ContentLength > 0 & isXLSXFile > 0)
{
string filePath = Path.Combine(
Server.MapPath("~/Uploads"),
Path.GetFileName("ExcelFileUpload.xlsx")); // save as new filename
try
{
file.SaveAs(filePath);
TempData["UploadValidationMessage_Success"] = "File upload succeeded";
return View();
}
catch (Exception e)
{
TempData["UploadValidationMessage_Failure"] = "Failed to upload the file " + e.ToString();
return View();
}
}
else
{
TempData["UploadValidationMessage_Failure"] = "Filename missing or filename extension is not .xlsx";
return View();
}
}
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
}