Unable to decode a URL string encoded by MVC - asp.net-mvc

I'm passing a string with a URL from the controller to the view (using a model).
While the string is in the controller, it is not encoded, but in the view the URL is encoded.
URL before encoding:
http://app.xpinator.com/FacebookPayments/FacebookDesktopAdData?paymentCode=s5usd920k&userLocale=en-us&ver=v3.0.1
URL after encoding:
http://localhost/FacebookPayments/FacebookPaymentItemData?paymentcode=v250usd45000k&userlocale=en-us&ver=v3.0.1
Relevant line in the view:
<meta property="og:url" content="#Model.URL" />
I want to display a decoded URL in the view. I tried using HttpUtility.HtmlDecode, HttpUtility.UrlDecode and Html.Raw - nothing worked.
Any ideas?
EDIT:
Thanks to Daniel's comment I realized that the encoding is happening only when the URL is in a meta property. when its a "displayable" HTML there is no encoding.
Anyway, still looking for a solution.

After realizing the problem occurs only with meta tags, I've found this thread:
Why is Html.Raw escaping ampersand in anchor tag in ASP.NET MVC 4?
I managed solve the problem with the suggested workaround to make the entire meta tag part of the raw value.
like this:
Controller:
model.URL = string.Format("{0}{1}{2}", "<meta property=\"og:url\" content=\"", paymentItemOG.URL, "\" />");
View:
<meta property="og:image" content="#Model.Image" />
#Html.Raw(Model.URL)
<meta property="og:description" content="#Model.Description" />
Page view-source:
<meta property="og:image" content="http://localhost/content/images/75x75_fullLogo.png" />
<meta property="og:url" content="http://localhost/FacebookPayments/FacebookPaymentItemData?paymentcode=v250usd45000k&userlocale=en-us&ver=v3.0.1" />
<meta property="og:description" content="texttexttext" />
Now it works great.

Related

When Tweeting my Wordpress post, how to get shortcode output to appear instead of shortcode text?

I use shortcodes on my WordPress site to help display reference information from my articles from a BibTeX file. For example, the article information at the top of my post is generated this way, using a shortcode of the form [bibtex key="id"], where id is the reference id in the BibTeX file. I am using the papercite plugin.
When I tweet this post, however, using the URL above, what I get instead is the shortcode text itself, instead of the shortcode output. See:
https://twitter.com/JDHamkins/status/1306124193357074433
Why is Twitter using the shortcode text here, and how to I get it to use the output text instead?
If you look at the source HTML, it has the lines
<!-- Jetpack Open Graph Tags -->
...
<meta property="og:description" content="[bibtex key=”HamkinsSolberg:Categorical-large-cardinals”]" />
...
<!-- End Jetpack Open Graph Tags -->
So it looks like the Jetpack WP plugin doesn't process the shortcodes.
I'd suggest to add an introductory sentence at the top of the post, before the [bibtex] line. Like maybe a 1-sentence summary of the abstract?
Update: Apparently the Yoast SEO Plugin can be used to solve the issue, see https://randomneuronsfiring.com/jetpack-publicize-setting-the-image-and-text-shown-on-twitter-and-facebook/
It will be because Twitter uses the <meta property="og:..."> tags in the header rather than using the page text:
<!-- Jetpack Open Graph Tags -->
<meta property="og:type" content="article" />
<meta property="og:title" content="Categorical large cardinals and the tension between categoricity and set-theoretic reflection" />
<meta property="og:url" content="http://jdh.hamkins.org/categorical-large-cardinals/" />
<meta property="og:description" content="[bibtex key=”HamkinsSolberg:Categorical-large-cardinals”]" />
<meta property="article:published_time" content="2020-09-16T06:50:00+00:00" />
<meta property="article:modified_time" content="2020-09-16T06:50:04+00:00" />
<meta property="og:site_name" content="Joel David Hamkins" />
<meta property="og:image" content="http://jdh.hamkins.org/wp-content/uploads/Categorical-cardinals-Venn-diagram.jpg" />
<meta property="og:image:width" content="504" />
<meta property="og:image:height" content="306" />
<meta property="og:locale" content="en_US" />
<meta name="twitter:site" content="#JDHamkins" />
<meta name="twitter:text:title" content="Categorical large cardinals and the tension between categoricity and set-theoretic reflection" />
<meta name="twitter:card" content="summary" />
JetPack offer some help — https://jetpack.com/tag/open-graph/ — on how to manually alter what their open graph plugin does, but it may take some work

Pound symbol display

I have an mvc solution with a standard view using _Layout page for layout. Layout page has charset=utf-8 set in the header like so:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
For some reason, when I type "£" symbol in the view I get it displayed as "A£", with a dash above "A". At the same time when I put the same symbol in the _Layout page it's displaying fine. I got this resolved by using encoded value
£
but was wondering why does it happen?
It may have happened because of a mismatch between <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> and the encoding of your physical file.
E.g. Your file could be saved as ISO-8859-1.
I have read that note. I am using Adobe Dreamweaver for all my files and I think it is encoded in 'utf-8'.

How to add media type to stylesheet reference asp mvc

I'm using asp mvc, and I'm using the following code to generate the CSS html reference:
#Styles.Render("~/Content/css")
which generates the following html:
<link href="/Content/site.css" rel="stylesheet"/>
and that works fine. However, I need to add media type as an additional attribute. How can I go about using this style.render to add attributes to the generated html? Should I be thinking about making the change in the bundle config instead?
edit: I would like the end product to look like this:
<link href="/Content/site.css" rel="stylesheet" media="handheld"/>
You should be using #Styles.RenderFormat() for that:
#Styles.RenderFormat(#"<link href=""{0}""
rel=""stylesheet""
media=""handheld"" />",
"~/Content/css")
Try this
< link href="#Styles.Url("~/Content/css")" rel="stylesheet" type="text/css" media="handheld" />

C# MVC Textbox MultiLanguage

I have a view with a TextBox for input. However it seems like it doesn't support all Spanish characters. The upside down question mark doesn't seem to work. Is there a simple way to get around this?
<%= Html.TextBoxFor(m => m.Product.Name, new { style = "width:400px", maxlength = 150 })%>
Things to check:
Inside your web.config you are using utf-8:
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
Inside the <head> section of your site you have a <meta> tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Your view files are utf-8 encoded with BOM on the disk

Grails interprets and closes HTML meta tag

In my Grails GSP file I'm using the HTML meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
The problem is that Grails closes this tag and renders it as:
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
This fails W3C's HTML validation (since my doctype is HTML and not XHTML).
Is there a fix for this? How can I get Grails to not interpret the
meta tag?
I'm using grails-1.2-M4.
Follow up:
I create the Grails bug GRAILS-5696 for this issue.
Not sure that this is the most beautiful solution, but at least it will work for your case:
<%= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' %>
Well...this does not work since it is preprocessed by Grails before displayed as is.
So the only solution I see is to create a TagLib and output the content like this:
class MetaTagLib {
static namespace = 'my'
def meta = {
out << "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>"
}
}
and use it like:
<my:meta />
It works. Tested.
You could validate as HTML5 instead of HTML 4.01, by using <!DOCTYPE html> (that's it, really!). HTML5 allows trailing slashes even in the HTML syntax, in order to allow for systems like this that produce pseudo-XHTML.
Of course, HTML5 is not yet a finished standard; it may change. I think that this aspect of it is unlikely to be changed, but there is still some fairly contentious debate about a lot of the new HTML5 features, so keep in mind that it's not yet finalized.

Resources