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.

Roman
  • 4,443
  • 14
  • 56
  • 81
  • Post the code for your action, your route, and what URL you're using to hit the action via said route – Michael Haren Feb 27 '11 at 14:15
  • @Michael - I've add the needed parameters. – Roman Feb 27 '11 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. – Nick Larsen Feb 27 '11 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? – Roman Feb 27 '11 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. – Nick Larsen Feb 27 '11 at 15:24
  • You could also post the relevant view code here and uncomment out the code in your action for us to see. – Nick Larsen Feb 27 '11 at 15:25
  • @Nick- I tried to just go to the link itself. no AJAX. still 2 calls. I've stepped through the code - I just get's to the return view, returns the view, but then goes to the action again and returns the second time's view (so I get the view from the second call, not the first one). There doesn't seem to be a double call for the action - because no body calls it. – Roman Feb 27 '11 at 15:33
  • @Nick - btw, the code is just an image resize code. nothing special - just pure C# code. – Roman Feb 27 '11 at 15:33
  • @roman are the parameters the same? is there any difference between the calls at all? – Nick Larsen Feb 27 '11 at 15:38
  • @Nick - nope. it seems like the same call. everything is identical. pretty strange. – Roman Feb 27 '11 at 15:42
  • roman: Do you use `Html.RenderAction` in your views? – Robert Koritnik Feb 27 '11 at 15:53
  • @robert - nothing :) just a plain view. no renderaction in the master page or the view. – Roman Feb 27 '11 at 15:55
  • @roman: What about your configuration? Have you duplicated anything in your config files? Have you changed anything in your config files? – Robert Koritnik Feb 27 '11 at 21:55

2 Answers2

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)

Community
  • 1
  • 1
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

CRice
  • 12,279
  • 7
  • 57
  • 84