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 = "V开发者_StackOverflowiew1" } // Parameter defaults
);
Link:
http://<some server>/View1/15/fb
Thanks.
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
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)
精彩评论