ura method

String ura(
  1. String internalPath, {
  2. bool? includeSession,
})

Rewrite an internal path and encode for an attribute.

It is very common to rewrite an internal URL and then put its value into an attribute. For example,

var link = "~/foo?a=1&b=2";
resp.write('<a href="${HEsc.attr(req.rewriteUrl(link))}">here</a>');

This convenience method invokes both rewriteUrl and HEsc.attr so the above can be simply written as:

resp.write('<a href="${req.ura(link)}">here</a>');

If used for the method attribute of a HTML form element, set includeSession to false and use the Request.sessionHiddenInputElement method inside the form element. See Request.sessionHiddenInputElement for more details.

Implementation

String ura(String internalPath, {bool? includeSession}) {
  // Rewrite

  String r;
  if (includeSession != null) {
    r = rewriteUrl(internalPath, includeSession: includeSession);
  } else {
    r = rewriteUrl(internalPath);
  }

  // Escape for use in a HTML attribute

  return HEsc.attr(r);
}