Creating Markup Actions in Razor

Razor’s inline helpers allow you to create lambda expression that return markup (as a HelperResult).  However, there is no simple way to create a lambda expression that writes HTML directly to the page (instead of returning it).

In ASPX pages, one can simply put the beginning and end of a lambda expression in expression blocks, then put markup in the middle.

For example, this code creates a delegate that writes a <b> tag to the page:

<%
    Action pageWriter = () => {%><b>I'm from a lambda!</b><%};
    pageWriter();
    pageWriter();
    pageWriter();
%>

Calling the pageWriter delegate will write directly to the HTTP response stream.

By contrast, Razor inline expressions return their markup.  To do this in a Razor page, one would write

@{
    Func<object, HelperResult> htmlMaker
         = @<b>I'm from a lambda!</b>;
    @htmlMaker(null)    //Note @ sign
    @htmlMaker(null)    //Note @ sign
    @htmlMaker(null)    //Note @ sign
}

Calling htmlMaker without an @ sign will return HTML, but won’t write anything to the page.

When working with libraries designed for ASPX pages, it can be necessary to create ASPX-style inline helpers that write to the page instead of returning a value.  You can do that by creating an inline helper lambda and passing it to the Write method:

@{
    Action pageWriter = () => Write(new Func<object, HelperResult>(
         @<b>I'm from a lambda!</b>
    )(null));

    pageWriter();
    pageWriter();
    pageWriter();
}

Like the ASPX version, pageWriter now writes directly to the page and does not return anything.

This code can be made simpler by wrapping it in a separate method:

Action MakeAction(Func<object, HelperResult> inlineHelper) {
    return () => Write(inlineHelper(null));
}

This method takes an inline helper and returns an Action that writes the helper’s output to the page.  Since it needs to call the page’s Write method (to use the page’s output stream), this method must be defined in the WebPageBase instance, either in an @functions block or in a common base class.

Any code in the inline helper will execute each time the resulting Action is called.

It can be called like this:

@{
    Action pageWriter2 = MakeAction(@<b>I'm from a lambda!</b>);

    pageWriter();
    pageWriter();
    pageWriter();
}

This code is equivalent to the previous sample, but it’s much simpler.

One can also write write a version that takes a parameter:

Action<T> MakeAction<T>(Func<T, HelperResult> inlineHelper) {
    return param => Write(inlineHelper(param));
}
Note that the type parameter must be specified explicitly; C# does not infer type parameters from the method’s return type.

13 comments:

Thanks for this information. I found it very useful in coming up with my own solution.

Just to explain what I wanted to do, I wanted to pass around markup just as you would pass around variables of any other type. However, sometimes when you use variables within a markup block and that variable changes (for example during a for-loop), it prints the last value that the variable was assigned (as opposed to the value it held at the time).

Therefore what I did was to strictly evaluate the markup passed in.

The following is my solution:

HtmlString Markup(Func<dynamic, HelperResult> markupExpr)
{
return new HtmlString(markupExpr(null).ToHtmlString());
}

and you can call it as such:

@{
var markup = Markup(@<span>...</span>);
}

(at this point you can put it in a list etc).

and then you can call it:

@markup

Nice and useful info. Thanks

beatrice

I recommend you to have a look here if you are bored. It will help you to make homework fun.

Thank you for sharing excellent information. Your website is so cool. I am impressed by the details that you have on this website.
office.com/setup
office/setup
Norton.com/setup

I am very happy after reading this fantastic Article, I appereciate your work.

norton.com/setup | www.norton.com/setup

Sign in to enter office setup product key. Know how to benefit, download, install, set in movement, uninstall and reinstall MS office setup.
office.com/setup
http://officecom-officeoffice.com/
call us at +1-888-421-9666[tool free]






If you can’t remember the account that you used, support might be able to help. Get Support; Do you have an Webroot Key code? Enter your product key; Don’t have Webroot yet? If you don’t have Webroot, you can try or buy it in the webroot.com. Buy now option. If you have an webroot subscription, on your safe page

webroot.com/safe
Office.com/Setup



Electricity is the set of physical phenomena associated with the presence and motion of matter that has a property of electric charge. In early days, electricity was considered as being unrelated to magnetism. Later on, many experimental results and the development of Maxwell's equations indicated that both electricity and magnetism are from a single phenomenon

what is electricity

Microsoft office.com/setup with your product key. Get started: step 1. Go to office.com/setup - Sign in,
2nd. Enter product key, and 3rd. Install Office 365 or Office 2016. If you are having any Office Setup issue then call us at 24*7 for technical help. we also help with like Excel, PowerPoint, PowerPoint,
Word, OneNote, ...all type issue ..We are also handing more lots of product .office.com/setup
California zip@code 90002
office.com/setup || office.com/setup

office.com/setup
www.office.com/setup
office.com/setup
office.com/myaccount

We are available 24/7 to help you out the complete procedure of download and install of office.com/setup. And also Check out here complete steps for mcafee.com/activate for downloading, installing, uninstalling, and activating, and also get antivirus help for norton.com/setup. For norton activation, get the 25 characters alphanumeric product key from Norton offers hordes of reliable antivirus programs

norton.com/setup gadgets that are enabled to access internet are vulnerable to online threats as norton.com/setup long as they don’t have any antivirus program installed to protect them against viruses and malware. No matter if you are a business person, freelancer, household, or an employee, Norton provides high-end security norton.com/setup infrastructure to every user. Make sure to only purchase the right product that matches your security obligations. As Norton offers you a trial version of every product online without implying any charges, you can easily get to know their performances before paying for the subscription plan. norton.com/setup You can get the details and access the trial version via norton.com/setup

We are providing online assistance to the customers for norton antivirus. If you want support from the expert's so, you can click on the following link and our expert's give you the best solution to your problem.
Office.com/setup
www.norton.com/setup
Norton.com/setup

Post a Comment