ASP.NET MVC - 双动作执行
我有一个在 ASP.NET MVC 2 上运行的网站 - 在一个操作上,我运行了一些代码,然后返回除操作名称之外的另一个视图。即 - action1 将返回视图“view2”。
不知何故,action1 运行一次,然后
return View("view2",model)
再次调用并运行,这是第二次。
为什么会这样呢?它可以修复吗?
编辑:添加了一些代码
Action:
public ActionResult View1(int id, int id2) {
// some code ...
return View("View2", u);
}
其中 View2 与 View1 或操作无关(仅用于显示)。
路线:
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
);
链接:
http://<some server>/View1/15/fb
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的默认路线放在底部。路由从上到下进行评估,因此...
http:///View1/15/fb
...由默认路由评估为Controller = Main,Action = View1
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
我也一直遇到和你一样的问题。
他们进行这种双动作执行有一些可能的原因。这里有一些有用的链接:
MVC 控制器被调用两次
ASP.NET MVC 操作被调用两次
在我的例子中是Chrome 的扩展名为“HTML Validator 1.3.3”。此分机正在再次调用该操作。 (我想进行验证)
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)