In Razor, you can check if the page is being redirected using the ViewContext.IsRedirected
property. This property returns a boolean value indicating whether the current page is being redirected. Here’s an example of how you can use it in Razor:
@if (ViewContext.IsRedirected)
{
<p>Page is being redirected.</p>
}
else
{
<p>Page is not being redirected.</p>
}
In the above code, the if
statement checks the value of ViewContext.IsRedirected
. If it’s true
, it means the page is being redirected, and a corresponding message is displayed. If it’s false
, it means the page is not being redirected, and a different message is displayed.
Note that ViewContext.IsRedirected
is only available within Razor views or layouts. If you need to check for a redirect in a different context, such as a controller, you can use Response.IsRequestBeingRedirected
in the controller action instead:
public ActionResult MyAction()
{
if (Response.IsRequestBeingRedirected)
{
// Handle redirect
}
else
{
// Continue normal processing
}
// Rest of the action code
return View();
}
In the above example, Response.IsRequestBeingRedirected
is used within a controller action to check if the request is being redirected. You can then handle the redirect accordingly or continue with the normal processing based on the result.