ASP.NET MVC无需物理重定向即可直接执行操作 [英] ASP.NET MVC Redirect to action without a physical redirect
问题描述
是否可以从任何动作,动作过滤器或其他任何方式以相同的方式在另一个控制器中执行另一个动作,而无需进行物理重定向.
Are it in any way possible to execute another action in the same on in another controller from an action, action filter or in any other way without doing a physical redirect.
之所以这样,是因为我有一个动态分页系统,例如,用户将在其中加载网址
The reason for this is that I have a dynamic paging system, where the user will load a url, for an example
/1/some-page-title
此URL映射到控制器"Home"和操作"Element",然后此操作将从数据库中加载元素ID为"1"的行.根据数据库中元素上的数据,页面将呈现为联系表单,图像库等. 现在我可以这样映射路径了
This url is maped to the controller "Home" and the action "Element", this action will then load a row from the database where the element id is "1". Depending from the data on the element from the database will the page be rendered as a contact form, an image gallery and so on. Now I could map the paths so
/Page/1/some-title/ will render a normal html page,
/Contact/1/some-title/ will render a contact form
/Gallery/1/some-title/ will render a gallery
但是我更希望路径是简单的.
But I would prefer the paths to be simple.
推荐答案
此答案存在问题,因为我对ASP MVC做过任何事情已有很长时间了,所以我实际上并不知道问题出在哪里. .
很遗憾,我无法删除接受的答案.
因此,我正在按原样敲击答案,如果您可以回答这个问题,或者让它变得更好,请这样做.
There are problems with this answer, it's been a long time since I did anything thing with ASP MVC, so I'm not actually aware of what the problems are.
Unfortunately I can't delete an accepted answer.
So, I'm striking through the answer as it was, if you can actually answer this, or make it better, please do so.
是的,真的很简单:)
Yes, very simple really :)
假设您在控制器C动作A中.要重定向"到控制器B动作Z,只需从当前动作中调用另一个控制器动作,并返回其结果即可.
Say you're in controller C action A. You want to "redirect" to controller B action Z, just call the other controller action from the current one, returning it's result.
public ActionResult A()
{
return B.Z()
}
这篇关于ASP.NET MVC无需物理重定向即可直接执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!