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>
....
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.
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With