在 ASP.NET MVC 中,返回 RedirectToAction 时保留 URL

发布于 2024-07-14 08:04:10 字数 1145 浏览 8 评论 0原文

我有一个操作方法,根据传递给它的内容,我想重定向到另一个控制器中的另一个操作。 操作和控制器名称在运行时确定。

如果我返回 RedirectToAction(),它将强制重定向并更改浏览器中的 URL。 我想要的是像 TransferToAction() 这样的东西,它可以将当前请求的处理转移到另一个操作,而不需要进行重定向。 我似乎记得在早期预览中有一个类似这样的方法,但我似乎在 ASP.NET MVC 的 RC 中找不到它。

你知道我会怎么做吗?

更新

我添加了以下路由:

routes.MapRoute(
    "PageRouter",
    "{site}/{*url}",
    new { controller = "PageRouter", 
          action = "RoutePage", site = "", url = "" }
);

PageRouter 控制器操作 RoutePage:

public ActionResult RoutePage(string site, string url)
{
    var controller = new HomeController {ControllerContext = ControllerContext};
    controller.RouteData.Values["controller"] = "Home";
    controller.RouteData.Values["action"] = "Index";

    return controller.Index(site, url);
}

我必须在 RouteData 中设置控制器和操作才能呈现“主页索引”视图。 否则,它将在 PageRouterController 中查找 Index 视图。

我仍然需要弄清楚如何创建一个控制器及其仅知道其名称的操作。 例如,我希望能够调用这样的内容:

public ActionResult RoutePage(string site, string url)
{
    return InvokeAction("Home", "Index");
}

InvokeAction() 中应该包含什么? 我需要传递任何上下文吗?

I have an action method, and depending on what is passed to it, I want to redirect to another action in another controller. The action and controller names are determined at run time.

If I return RedirectToAction(), it will force a redirect and change the URL in the browser. What I would like is something like TransferToAction() that can transfer processing of the current request to another action, without doing a redirect. I seem to remember a method behaving like this in earlier previews, but I can't seem to find it in the RC of ASP.NET MVC.

Do you know how I would do this?

UPDATE

I added the following route:

routes.MapRoute(
    "PageRouter",
    "{site}/{*url}",
    new { controller = "PageRouter", 
          action = "RoutePage", site = "", url = "" }
);

And the PageRouter controller action RoutePage:

public ActionResult RoutePage(string site, string url)
{
    var controller = new HomeController {ControllerContext = ControllerContext};
    controller.RouteData.Values["controller"] = "Home";
    controller.RouteData.Values["action"] = "Index";

    return controller.Index(site, url);
}

I had to set the controller and action in RouteData for the Home Index view to be rendered. Otherwise, it would look for an Index view in PageRouterController.

I still need to figure out how to create a controller and its action knowing only their names. e.g. I'd like to be able to just call something like this:

public ActionResult RoutePage(string site, string url)
{
    return InvokeAction("Home", "Index");
}

What should go in InvokeAction() ? Do I need to pass it any context?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掀纱窥君容 2024-07-21 08:04:10

您应该能够直接调用另一个方法,假设它返回一个 ViewResult,它将呈现该视图以响应请求,并且 url 不会更改。 请注意,您将负责确保其他方法所需的所有数据可供其使用。 例如,如果您的其他方法需要一些未提供的表单参数,您可能需要构造一个合适的 FormCollection 并将控制器的 ValueProvider 设置为基于您的 FormCollection 的 ValueProvider。 对于该方法所需的任何参数也是如此。

You should be able to just call the other method directly and, assuming that it returns a ViewResult, it will render that view in response to the request and the url will not change. Note, you'll be responsible for making sure that all of the data that the other method needs is available to it. For example if your other method requires some form parameters that weren't provided, you may need to construct a suitable FormCollection and set the ValueProvider of the controller to a ValueProvider based on your FormCollection. Likewise with any arguments required by the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文