Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abort/skip/cancel the rendering of a Razor view

Tags:

Many devs do this:

public void foo() {   if (flag) {     // do stuff   } } 

I prefer to "return early", and so do this instead:

public void foo() {    if (!flag) return;    // do stuff  } 

In an ASP.NET-MVC Razor view, what is the correct way to abort/skip/cancel rendering of a view/partialview? For example how do I convert this:

@if (flag) {   // do stuff } 

to something like this:

@if (!flag) { /* what do I do here to abort/skip/cancel the rendering? */ }  // do stuff 

I've tried using return and playing with the Request, but am unsure how this affects the process. What is the correct way to do this?

like image 835
h bob Avatar asked Sep 09 '14 10:09

h bob


People also ask

How do you escape the razor page?

In Razor, `@` symbol is used to transition from HTML to C#. To escape an '@' symbol in razor markup, use two '@' symbols.

What is ViewData in razor?

ViewData is a container for data to be passed from the PageModel to the content page. ViewData is a dictionary of objects with a string-based key. You add items to ViewData as follows: public class IndexModel : PageModel. {

What is MVC Razor?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.

What is Razor syntax in C#?

Razor is a markup syntax for embedding . NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a . cshtml file extension.


2 Answers

As I mentioned above, you can simply issue a return.

I recall cshtml files are compiled at runtime, which includes all inline code as well as the static html. That means that in theory, I'd expect any code to be left as-is and not transformed in any way.

So if you do this:

@if (!flag) { return; }  // do stuff 

It works as expected (well for me at least). I just wanted to know if this leads to any unintended side-effects.

like image 70
h bob Avatar answered Sep 20 '22 06:09

h bob


If with "abort" you mean you want to exclude something according to that flag then you have nothing to do, just use if to delimit such sections (eventually it may even be from that line to the end of file). Like this:

 @if (Model.User.HasEditingPrivileges)  {      <input type="button" id="edit" value="Edit"/>  } 

Quickly it'll become hard to understand so you may use partial views for that (especially if blocks you have to include/exclude are big):

 @if (Model.User.HasEditingPrivileges)  {      Html.RenderPartial("EditSection");  } 

If you need to drop/skip/cancel page creation (for example to redirect to another page or to display something completely different), like this:

@if (!Model.User.hasEditingPrivileges) {     // Ooops, he shouldn't see this page, go back to Home! } 

Then you're doing that check in the wrong place. Do it in your controller, it'll pick right view and views won't be aware of such logic:

public ActionResult View(int id) {     if (HasUserEditingPrivileges)         return View("Edit", new MyModel(id));      return Redirect("UnauthorizedAccess"); // Oops, something went wrong } 

Why not?

  • Because views shouldn't be aware of such logic. If you need something like that (CGI like sequential flow) then you shouldn't use MVC because it adds a complexity you don't need.
  • Because you can't (unless someone find a terrible dirty hacky trick). MVC is structured to build a page when it has to be displayed (controller decides which page and with which data). When this building starts an output is required (unless you throw an exception to signal an error but you should really avoid exceptions to handle program flow then...). For small ifs you may simply use first mentioned method.

To summarize:

In an ASP.NET-MVC Razor view, what is the correct way to abort rendering of a view/partialview?

There is not a correcty way because you must not do it. Code in view has to render the page, it has not to decide which page should be sent to client. If you're doing anything else then you're using MVC as it shouldn't be used, it's just not correct.

like image 28
Adriano Repetti Avatar answered Sep 22 '22 06:09

Adriano Repetti