0

All, I encountered a problem which MVC action is called twice. Please help to review it.

The view code is simple.

<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.
    }
}

Since the LogBlobController belong to an MVC Area named Log. So the route config in the AreaRegistration looks like below.

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 }
            );
        }
    }

And the default implement of RouteConfig of MVC is below.

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 }
            );
        }

And the Html in the page looks like below.

<a href="/Log/LogBlob/Delete?blobUrl=%2Fserverlogcontainer%2F1a33f37c-4b21-41aa-9f48-0b5f6b86577f.zip">Delete</a>

I doubted the reason of twice is they(LogAreaRegistration and RouteConfig) both worked at the same time. thanks.

Joe.wang
  • 11,537
  • 25
  • 103
  • 180
  • No , I didn't add any dom event like `click` on the link. – Joe.wang May 02 '14 at 09:20
  • You still never know... download [Fiddler](http://www.telerik.com/fiddler) if you don't have it already, monitor the Http requests to see if there are **2** calls to the action. Also experiment with explicitly registering a route or use the `[Route]` attribute on your action. – Aydin Jun 28 '14 at 03:26
  • this sulotion is good for your problem: https://stackoverflow.com/questions/1751266/asp-net-mvc-action-is-called-twice – habiat Nov 12 '18 at 13:13

0 Answers0