所有,我遇到了一个 MVC 动作被调用两次的问题。请帮忙审核。
视图代码很简单。
<div class="divContainer">
<ul>
@foreach (var blobName in ViewBag.BlobList)
{
<li>@Html.ActionLink("Delete", "Delete", "LogBlob", new { blobUrl = blobName }, null)</li>
}
</ul>
</div>
public class LogBlobController : Controller
{
public ActionResult Delete(string blobUrl)
{
//...
//The action is call twice.
}
}
由于LogBlobController
属于一个名为Log
. 所以下面的路由配置AreaRegistration
如下所示。
public class LogAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Log";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Log_default",
"Log/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
MVC的默认实现RouteConfig
如下。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Login", id = UrlParameter.Optional }
);
}
页面中的 Html 如下所示。
<a href="/Log/LogBlob/Delete?blobUrl=%2Fserverlogcontainer%2F1a33f37c-4b21-41aa-9f48-0b5f6b86577f.zip">Delete</a>
我怀疑两次的原因是他们(LogAreaRegistration
和RouteConfig
)都同时工作。谢谢。