如何判断是否从另一个控制器动作调用控制器动作?

时间:2023-01-29 21:13:14

Is there any easy way to distinguish between an ASP.NET MVC controller action being hit "directly" due to a client web browser request, and being hit by virtue of a Controller.RedirectToAction call or a RedirectToRoute result?

有什么简单的方法可以区分ASP。NET MVC控制器动作由于客户端web浏览器请求而被“直接”命中,并且通过控制器被命中。RedirectToAction调用还是RedirectToRoute结果?

3 个解决方案

#1


1  

Alternatively, put a value in TempData

或者,在TempData中输入一个值

public class SomeController : Controller
{
    public ActionResult SomeAction()
    {
        // ... do stuff ...
        TempData["SomeKey"] = "SomeController.SomeAction";
        return RedirectToAction("SomeOtherAction", "SomeOtherController");
    }
}

public class SomeOtherController : Controller
{
    public ActionResult SomeOtherAction()
    {
        if (TempData.ContainsKey("SomeKey"))
        {
            // ... do stuff ...
        }
        // etc...
    }
}

(From Craig Stuntz)

(从克雷格时)

#2


1  

You may have the option of adding a parameter to your Action method that allows you to pass in a value specifying whether it's a Controller.RedirectToAction, a RedirectToRoute, or a client browser request. Couple this with some server variable checks and you may be able to come up with something that works most of the time.

您可以选择向操作方法添加一个参数,该参数允许您传入一个值,该值指定它是否是控制器。RedirectToAction、RedirectToRoute或客户端浏览器请求。将它与一些服务器变量检查结合在一起,您可能会得到一些大多数时候都有效的东西。

public ActionResult MyAction(string source)
{
    if (source == "")
    {
        // client browser request
    }
    else if (source == "redirectToAction")
    {
        // redirect to action
    }
    else if (source == "redirectToRoute")
    {
        // redirect to route
    }
}

#3


0  

Request.ServerVariables["http_referrer"] would be empty if is the action is hit from a redirect to action, I think. Otherwise it would be the URL that corresponds to the action method visited "directly".

请求。如果操作从重定向到操作被命中,那么服务器变量["http_referrer"]将是空的。否则,它将是对应于“直接”访问的操作方法的URL。

Kindness,

善良,

Dan

#1


1  

Alternatively, put a value in TempData

或者,在TempData中输入一个值

public class SomeController : Controller
{
    public ActionResult SomeAction()
    {
        // ... do stuff ...
        TempData["SomeKey"] = "SomeController.SomeAction";
        return RedirectToAction("SomeOtherAction", "SomeOtherController");
    }
}

public class SomeOtherController : Controller
{
    public ActionResult SomeOtherAction()
    {
        if (TempData.ContainsKey("SomeKey"))
        {
            // ... do stuff ...
        }
        // etc...
    }
}

(From Craig Stuntz)

(从克雷格时)

#2


1  

You may have the option of adding a parameter to your Action method that allows you to pass in a value specifying whether it's a Controller.RedirectToAction, a RedirectToRoute, or a client browser request. Couple this with some server variable checks and you may be able to come up with something that works most of the time.

您可以选择向操作方法添加一个参数,该参数允许您传入一个值,该值指定它是否是控制器。RedirectToAction、RedirectToRoute或客户端浏览器请求。将它与一些服务器变量检查结合在一起,您可能会得到一些大多数时候都有效的东西。

public ActionResult MyAction(string source)
{
    if (source == "")
    {
        // client browser request
    }
    else if (source == "redirectToAction")
    {
        // redirect to action
    }
    else if (source == "redirectToRoute")
    {
        // redirect to route
    }
}

#3


0  

Request.ServerVariables["http_referrer"] would be empty if is the action is hit from a redirect to action, I think. Otherwise it would be the URL that corresponds to the action method visited "directly".

请求。如果操作从重定向到操作被命中,那么服务器变量["http_referrer"]将是空的。否则,它将是对应于“直接”访问的操作方法的URL。

Kindness,

善良,

Dan