0

I've got a website running on ASP.NET MVC 2 - on one action I have some code running and then returning ANOTHER view than the action's name. i.e - action1 will return view "view2".

Somehow, action1 runs one time, then calls

return View("view2",model)

and runs again, for the second time.

Why is this so? and can it be fixed?

EDIT: added some code

Action:

public ActionResult View1(int id, int id2) {
// some code ...
return View("View2", u);
}

where as View2 has nothing to do with View1 or the action (just needed for display).

Route:

    routes.MapRoute(
        "Default", // Route name
        "{action}/{id}", // URL with parameters
        new { controller = "Main", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );


    routes.MapRoute(
        "View1", // Route name
        "View1/{id}/{id2}", // URL with parameters
        new { controller = "Main", action = "View1" } // Parameter defaults
    );

Link:

http://<some server>/View1/15/fb

Thanks.

13
  • Post the code for your action, your route, and what URL you're using to hit the action via said route Commented Feb 27, 2011 at 14:15
  • @Michael - I've add the needed parameters. Commented Feb 27, 2011 at 14:21
  • 1
    @roman the only reason an action would get called twice is if there are 2 requests for that url or code called that function during its execution. Neither of these reasons can be deduced from the information you have provided us. If you step through the code and call comes after the first one returns, you are almost certainly generating a second request. Commented Feb 27, 2011 at 15:18
  • 1
    @NickLarsen - that was also my thought, but there is no evidence that it is being called twice. Is there anyway I could find out what calls it twice? Commented Feb 27, 2011 at 15:20
  • @roman you could watch it with proxy like fiddler. If you step through the code and the action returns a result, then the action is called again, you are almost without question getting 2 requests. And AJAX call could cause this, so could a redirect result or opening the site in 2 browsers or pressing refresh on the one you have it open in, amongst other reasons. Commented Feb 27, 2011 at 15:24

2 Answers 2

0

Put your default route at the bottom. Routes are evaluated top to bottom so ...

http://<some server>/View1/15/fb

... gets evaluated by the default route as Controller = Main, Action = View1

Sign up to request clarification or add additional context in comments.

Comments

0

I have been having the same problem like you.

There are some possible reasons they are producing this double action execution. There you are some useful links:

MVC controller is being called twice

ASP.NET MVC Action is Called Twice

In my case was an extension for chrome called "HTML Validator 1.3.3". This extension was calling the action again. (I guess to do the validation)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.