ASP.NET MVC Redirect to action without a physical

2019-04-27 05:17发布

问题:

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

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.

回答1:

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 :)

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()
}



回答2:

You may be looking for Html.RenderAction or Html.Action. However, these are used in the view and not the controller.