动作被调用两次

时间:2014-05-02 09:05:45

标签: asp.net-mvc-4 asp.net-mvc-routing asp.net-mvc-5 asp.net-apicontroller

所有,我遇到了两次调用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>

我怀疑两次的原因是他们(LogAreaRegistrationRouteConfig)同时工作。感谢。

0 个答案:

没有答案