Get full URL from (virtual) tilde referenced path - asp.net-core

How do I get the full URL string when using the relative path nomenclature via tilde? For example, if I want to refer to something in my view using relative path...
~/images/mylogo.jpg
I would like to get the full url such that it would end up looking like ...
https://myserver:8081/images/mylogo.jpg
This is needed to populate the metatags for Open Graph. I am trying to use ...
<meta property="og:image" content="#Url.Content("~/images/mylogo.jpg")" />
... but this only gives relative paths resulting in ...
<meta content="/images/mylogo.jpg" property="og:image">
Details - need to detect if SSL is used or not. Need to determine current port and host name...

The possible duplicate answer at Getting absolute URLs using ASP.NET Core MVC 6 was very similar, but did not address the use of the tilde. For the tilde I used the URL Helper 'Content' method. For completeness here is where I landed...
Create extension method...
using Microsoft.AspNetCore.Mvc;
namespace testProject.Utilities
{
public static class MVCExtensionMethods
{
public static string BaseUrl(this IUrlHelper helper)
{
var url = string.Format("{0}://{1}", helper.ActionContext.HttpContext.Request.Scheme, helper.ActionContext.HttpContext.Request.Host.ToUriComponent());
return url;
}
public static string FullURL(this IUrlHelper helper, string virtualPath)
{
var url = string.Format("{0}://{1}{2}", helper.ActionContext.HttpContext.Request.Scheme, helper.ActionContext.HttpContext.Request.Host.ToUriComponent(), helper.Content(virtualPath));
return url;
}
}
}
Use in View via Razor...
#using testProject.Utilities
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta property="og:url" content="#Url.BaseUrl()" />
<meta property="og:type" content="website" />
<meta property="og:title" content="#ApplicationConstants.ApplicationTitle" />
<meta property="og:description" content="#ApplicationConstants.TagLine" />
<meta property="og:image" content="#Url.FullURL("~/images/logo-black.png")" />
<title>#ApplicationConstants.ApplicationTitle</title>
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
#Html.ApplicationInsightsJavaScript(TelemetryConfiguration)
</head>
<body>
</body>
</html>
Summary...
MVC has two kinds of helpers - HTML Helpers and URL helpers. I was trying to get the URL when using the HTML helpers. Should have been using the URL helper instead. The possible dup answer directed me to look into the URL helper instead. It did not show the use of the tilde virtual directory nomenclature though...

Related

meta tag fixed for each route

is there any way for me to change the tags meta according to the page I'm accessing an example below
Home
/
<meta property="twitter:card" content="summary_large_image">
<meta property="twitter:url" content="https://website.com.br/">
<meta property="twitter:title" content="Website - Home">
<meta property="twitter:description" content="Website online">
<meta property="twitter:image" content="https://website.com.br/logo.jpg">
/product/123
<meta property="twitter:card" content="summary_large_image">
<meta property="twitter:url" content="https://website.com.br/product/123">
<meta property="twitter:title" content="Website - Product 123">
<meta property="twitter:description" content="Description product">
<meta property="twitter:image" content="https://website.com.br/product/123/thumbnail.jpg">
I used the vue-meta but didn't get the expected result to work the browser needs before accessing and waiting for JavaScript to do its job, but some social bots just consumes the HTML not running JavaScript that would make the change.
Is there any way I can fix the meta for each page?

How can i use a template in the email body for Microsoft graph API?

I am looking to use the source code of an email which has the desired template that i need in the Microsoft Graph API.
The standard format for this looks like so:
"message": {
"subject": "Meet for lunch?",
"body": {
"contentType": "HTML",
"content": "The new cafeteria is open."
},
The sourcecodes template that i am looking to use already has contentType etc within the code.I have tried removing contentType and content and just having the source code within the body but this still does not work. Below is start of the source code that i am looking to use:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--[if !mso]>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="http://www.opusenergy.com/wp-content/uploads/2016/03/Favicon-150x150.png" />
<title>Opus</title>
<!--[if (gte mso 9)|(IE)]>
<style type="text/css">
table {border-collapse: collapse !important;}
</style>
Any help will be greatly appreciated, Thank you.
The above payload is not what Graph API looks for. You need to specify the payload for the create message API call and the structure is given in the Graph API document. Still if you want to customize, you can do it, by making sure to modify/customize in-lines with Microsoft Graph API recommendation. Say i will put the HTML content inside the content of the Graph API call, specify the contenttype as HTML. This is the way i would start testing it make sure whether it's working or still i need to modify so that the message can show correctly in Outlook or not.
You tried the above recommendation and confirmed that it works.
If i want to send out email/template then i would first make sure it works in Word/Outlook; so that you can validate the HTML/CSS tags are working in, as i know that not all tags are not supported. Please keep this best practice and plan it accordingly. It will help you to build the template as you wish and you're guaranteed that the Outlook will show-up them as well.

Extra HTML meta data tags in branch.io generated links

We are using branch to generate dynamic links for our application. We are using these links on various social media platforms. Branch configuration options enables us to populate fixed set of open graph data. In case of videos it lets us set og:video but there is no way to set og:video:type. Due to this, video embedding doesn't work if you are using HTML5 videos as the default video-type for Facebook is application/x-shockwave-flash.
I tried passing extra data while configuring link but it doesn't translate to HTML meta tags that are scrapped by Facebook.
When a Branch link is scraped, Branch will return:
1st: any parameters that have been defined for the link;
2nd: any parameters that have not been defined for the link but that have been defined at the app level (in Social Media Display Customization on the Link Settings page);
and finally: any meta tags present on the web site specified in the Default URL ($fallback_url) page.
Thus, even though the Branch does not provide an explicit parameter for defining og:video:type, you should be able accomplish this by adding the meta tag to the web page you use as your $fallback_url.
Here is an example:
I start with the following link "https://ogt1.app.link/fallbackurltags" where I have set the parameters below (note that "" is shown because I did not set any OG parameters for the link). Note that I have set the $fallback_url:
{
"$desktop_deepview": "",
"$desktop_url": "",
"$fallback_url": "https://dwestgate.github.io/ExampleFallBackURL/",
"$marketing_title": "OG Tag Testing - fallback URL tags",
"$og_description": "",
"$og_image_url": "",
"$og_title": "",
"$one_time_use": "",
"~creation_source": 1,
"~feature": "marketing",
"~id": "357178398369800081",
"~marketing": true
}
"og:video:type=application/x-shockwave-flash" has not been seet anywhere on the Branch dashboard or as a link parameter.
I have added meta tags, however, to a simple web site I have created and assigned as the $fallback_url: https://dwestgate.github.io/ExampleFallBackURL/. Here is that web page:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="title" content="Tag set by Fallback URL" />
<meta name="description" content="Tag set by Fallback URL" />
<meta property="og:title" content="Tag set by Fallback URL" />
<meta property="og:video:url" content="http://www.youtube.com/v/CsGYh8AacgY?version=3&autohide=1" />
<meta property="og:video:type" content="application/x-shockwave-flash" />
<meta property="og:video:width" content="160" />
<meta property="og:video:height" content="90" />
<title>Fallback URL with meta tags</title>
</head>
<body>
<h1>All the action is in the meta tags - View Source</h1>
</body>
</html>
When I now browse to Facebook's Sharing Debugger (here: https://developers.facebook.com/tools/debug/sharing/?q=https%3A%2F%2Fogt1.app.link%2Ffallbackurltags) - voila: the og:video:type tag from that web page has been scraped. You can see the raw tags returned by clicking on the "Show All Raw Tags" button.
One thing that I did not mention that may screw things up: if there is a value provided for $og_url, only the tags for the website provided in this value will be present. The link https://ogt1.app.link/linktags, for example, has the following parameters:
{
"$marketing_title": "OG Tag Testing - per-link tags",
"$og_description": "OG Tags set at the link level",
"$og_image_height": "200",
"$og_image_url": "https://cdn.branch.io/branch-assets/1486394827408-og_image.png",
"$og_image_width": "200",
"$og_title": "Per-Link tags",
"$og_url": "https://dwestgate.github.io/ExampleFallBackURL/",
"$og_video": "https://www.youtube.com/v/CsGYh8AacgY",
"$og_video_height": "180",
"$og_video_width": "320",
"$one_time_use": "",
"~creation_source": 1,
"~feature": "marketing",
"~id": "357170874539266599",
"~marketing": true
}
In this situation the link parameters get overridden by the parameters in $og_url, as can be seen by checking the Facebook Sharing Debugger again (here: https://developers.facebook.com/tools/debug/sharing/?q=https%3A%2F%2Fogt1.app.link%2Flinktags):
<meta charset="utf-8" />
<meta name="title" content="Tag set by Fallback URL" />
<meta name="description" content="Tag set by Fallback URL" />
<meta property="og:title" content="Tag set by Fallback URL" />
<meta property="og:video:url" content="http://www.youtube.com/v/CsGYh8AacgY?version=3&autohide=1" />
<meta property="og:video:type" content="application/x-shockwave-flash" />
<meta property="og:video:width" content="160" />
<meta property="og:video:height" content="90" />
I hope this provides sufficient clarity to allow you to populate whatever custom OG tags are needed.
There is an option to add custom meta tags according to branch documentation. Just put every meta tag inside it which aren't supported by default.
$custom_meta_tags: '{"og:video:type": "application/x-shockwave-flash"}'

(Dojo 1.10) URL returned by dojoLoader seems wrong/inconsistent, how can I configure this correctly?

We recently updated our server with signed certificates and changed the base URL from https://servername/ to https://servername.domain.info/ . This has broken module loading in Dojo and I can't seem to correct it. The current config looks like this:
var dojoConfig = {
async: true,
packages: [
{
name: "js",
location: location.pathname.replace(/\/[^/]+$/, "") + 'Scripts'
},
{
name: "widgets",
location: location.pathname.replace(/\/[^/]+$/, "") + 'Scripts/widgets'
},
{
name: "dgrid",
location: location.pathname.replace(/\/[^/]+$/, "") + 'Scripts/3.14/dgrid'
},
{
name: "dijit",
location: location.pathname.replace(/\/[^/]+$/, "") + 'Scripts/3.14/dijit'
}
]
};
This used to work, and it still works on my dev machine when the root URL is just 'localhost'. Looking at the errors in the console it seems to be trying to find the modules at https://servername/ instead of https://servername.domain.info/ and I've tried everything I could find to point it in the right direction, including setting baseUrl: 'https://servername.domain.info' explicitly. Anything I've tried results in different URL strings, sometimes just the pathname and no base URL, sometimes pathname plus protocol with no domain info, etc. For reference, I also tried just adding the server URL to each location's URL string and I get the same error that seems to drop all but the first word from the server domain name.
I'm not sure what I'm doing wrong and I'm baffled by what dojoLoader decides to drop. I've read and reread the documentation but not much is said about how the base URL is calculated as far as I can find, just stuff on how to set baseUrl in the config to something.
edit: Per the comment below, I realize more info is needed. I am using Visual Studio 2015 and publishing to Windows Server 2012 R2 running IIS (v7 iirc). It does look like maybe Visual Studio is messing with the URL somehow, Dojo is not to blame. I was able to work around it by editing the file directly on the server and setting a location URL for every package (and their dependencies). I'd still like to know why it's happening and how I can set it up correctly.
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>#ViewBag.Title - ProjectName</title>
<link rel="stylesheet" href="~/Content/font-awesome.min.css" />
<link rel="stylesheet" href="~/Content/jquery-ui-1.10.3.custom.css" />
<link rel="stylesheet" href="~/Scripts/3.14/esri/css/esri.css" />
<link rel="stylesheet" href="~/Content/bootstrap-datetimepicker.min.css" />
#Scripts.Render("~/bundles/modernizr")
<link rel="stylesheet" href="~/Content/map.css" />
#Styles.Render("~/Content/css")
</head>

Declaring Variables in DOJO

I am writing a JSP that displays a list of clubs in a grid. The grid shows the name of the club together with its latitude, longitude, website and description.
The actual data to be displayed is stored in a variable (a dojo.data.ItemFileWriteStore) called clubStore.
When the page is loaded, a call is made to a servlet to retrieve the data. The handling function then deletes all the items held in the store and adds new items returned by the servlet.
The JSP code is shown below:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Clubs</title>
<style type="text/css">
#import "./dojoroot/dojo/resources/dojo.css";
#import "./dojoroot/dijit/themes/tundra/tundra.css";
#import "./dojoroot/dojox/grid/resources/Grid.css";
#import "./dojoroot/dojox/grid/resources/nihiloGrid.css";
</style>
<script type="text/javascript" src="dojoroot/dojo/dojo.js"
djConfig="parseOnLoad: true, isDebug: false">
</script>
<script language="JavaScript" type="text/javascript">
dojo.require("dojo.parser");
dojo.require("dojo.data.ItemFileWriteStore");
var clubData={
items:[{name:'No Clubs', lat:'---', lon:'---', webSite:'---', description:'---'}]
};
var layoutClub=[{field:"name", name:"Name", width:10},
{field:"lat", name:"Lat", width:5},
{field:"lon", name:"Long", width:5},
{field:"webSite", name:"Web Site", width:10},
{field:"description", name:"Description", width:'auto'}];
var clubStore=new dojo.data.ItemFileWriteStore(data:clubData});
</script>
<link rel="stylesheet" href="dojoroot/dijit/themes/claro/claro.css" />
<link rel="stylesheet" href="dojoroot/dojox/widget/Dialog/Dialog.css" />
</head>
<body class="tundra">
<%#include file="header.jsp"%>
<div id="clubGrid"
style="width: 800px;"
autoHeight="true"
data-dojo-type="dojox/grid/DataGrid"
data-dojo-props="store:clubStore,
structure:layoutClub,
query:{},
queryOptions:{'deep':true},
rowsPerPage:40">
</div>
<br>
<script>
var urlString="http://localhost:8080/BasicWeb/ClubsServlet";
dojo.xhrGet({
url: urlString,
handleAs: "text",
load: function(data) {
// remove items...
var allData=clubStore._arrayOfAllItems;
for (i=0; i<allData.length; i++) {
if (allData[i]!=null) {
clubStore.deleteItem(allData[i]);
}
}
var jsonClubArray=JSON.parse(data);
for (var i=0; i<jsonClubArray.clubs.length; i++) {
var club=jsonClubArray.clubs[i];
var newClub={name: club.clubname, lat:club.lat, lon:club.lon, webSite: club.website, description: club.description};
clubStore.newItem(newClub);
}
clubStore.save();
}
});
</script>
</body>
</html>
The script to process the servlet response sometimes fails because clubStore is undefined (debugging using Firebug). This does seem to be a spurious fault as some times everything works perfectly.
Any assistance in understanding how to define the clubStore variable would be appreciated.
Thanks.
James.
I think what might be happening is the body script is sometimes running before the head script, so it is kind of a race condition. You could try wrapping your body script into a dojo.ready. (I assume from your code that you are using dojo 1.6 or earlier since you are not using the AMD loader style.)
dojo.ready(function(){
// Put your xhr request code here.
});
You may also want to try testing with a firebug breakpoint in the head and body script. See if the head is sometimes running first.
So the problem turned out to be a syntax error in the declaration - missing '{' in the line
var clubStore=new dojo.data.ItemFileWriteStore(data:clubData});
The spurious aspect to the fault was a red herring - I had previously declared the variable as part of the DOM object and that caused a spurious fault. So I messed up my regression testing as well as introducing a syntax error!
Thanks.
James.
You could try switching the order of your require statements, so it's like this:
dojo.require("dojo.data.ItemFileWriteStore");
dojo.require("dojo.parser");
If that fails, you could set parseOnLoad to false, and then call dojo.parser.parse() after your store has been instantiated like so:
(assuming you are using dojo 1.6 or earlier based on your code)
dojo.addOnLoad(function() {
dojo.parser.parse();
});
Put your clubStore in the global space... just remove the var keyword in front of it...