所有,我遇到了两次调用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
的MVC区域。所以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
)同时工作。感谢。