【问题标题】:calling return RedirectToAction("Activity") outside controller在控制器外调用 return RedirectToAction("Activity")
【发布时间】:2011-08-11 09:43:00
【问题描述】:

我正在构建一个流畅的界面,并想在我的控制器之外调用下面的代码......

return RedirectToAction("Activity");

我将如何设计这种方法?我有:

    public FluentCommand RedirectOnSuccess(string url)
    {
        if (IsSuccess)
            ;// make call here...

        return this;
    }

注意:IsSuccess 在这里设置:

public FluentCommand Execute()
        {
            try
            {
                _command.Execute();
                IsSuccess = true;
            }
            catch (RulesException ex)
            {
                ex.CopyTo(_ms);
                IsSuccess = false;
            }
            return this;
        }

我调用我的流利界面:

var fluent = new FluentCommand(new UpdateCommand(param,controller,modelstate)
.Execute()
.RedirectOnSucess("Actionname");

【问题讨论】:

    标签: c# asp.net-mvc-2


    【解决方案1】:

    您可以将HttpContextBase 的实例存储为流利界面中的字段以及需要重定向时:

    var rc = new RequestContext(context, new RouteData());
    var urlHelper = new UrlHelper(rc);
    context.Response.Redirect(urlHelper.Action(actionName), false);
    

    【讨论】:

    • 谢谢,我已经传入控制器并将其存储为字段并简单地使用了_controller.HttpContext。
    • 出于好奇,我是否使用了重载: var urlHelper = new UrlHelper(rc);传递路线数据?例如taskid = 34
    • @Haroon,你可以这样使用它:urlHelper.Action(actionName, new { id = taskid })
    • 可以使用var context = new HttpContextWrapper(HttpContext.Current)获取当前的HttpBaseContext。 (stackoverflow.com/a/3472045/37147)
    • 问题中的代码示例期望返回值。但解决方案不会有返回构造(重定向无效)。
    猜你喜欢
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    相关资源
    最近更新 更多