Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop ASP.NET MVC view rendering inside cshtml?

What is proper way to stop cshtml view rendering after redirect? Can I do return inside cshtml?

@{
 if (someCondition) {       
        Response.Redirect("/login");
        return;
    }
 }
}
<!DOCTYPE html>
....
like image 553
Alexey Ryazhskikh Avatar asked Jun 24 '15 17:06

Alexey Ryazhskikh


People also ask

What is _ViewImports Cshtml?

The _ViewImports. cshtml file for an ASP.NET Core MVC app is typically placed in the Pages (or Views) folder. A _ViewImports. cshtml file can be placed within any folder, in which case it will only be applied to pages or views within that folder and its subfolders.

How does _layout Cshtml work?

So, the _layout. cshtml would be a layout view of all the views included in Views and its subfolders. The _ViewStart. cshtml can also be created in the sub-folders of the View folder to set the default layout page for all the views included in that particular subfolder.

What does return View () in MVC do?

The default behavior of the View method ( return View(); ) is to return a view with the same name as the action method from which it's called. For example, the About ActionResult method name of the controller is used to search for a view file named About.


1 Answers

I believe this may be a duplicate question, to which the short answer is:

It's perfectly valid to return; from within cshtml; however there is usually a better way.

Sources:

  • Abort/skip/cancel the rendering of a Razor view
  • ASP.NET MVC3 Razor - How to conditionally quit or end or return or break a partial view?
  • Is 'Exit Sub' from within a razor view a concern?
like image 185
Jonathan Avatar answered Oct 19 '22 05:10

Jonathan