I am trying to get the path to my root project directory in layout.cshtml
, and pass it into Javascript as a global variable for use on ajax calls. This is what I thought would work, but I am getting an empty string as the value:
var basePath = Url.Content("~");
is returning empty string.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - ButtaLove</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Cookie">
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<link rel="stylesheet" href="~/lib/lightslider/css/lightslider.min.css">
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="~/ButtaLove.styles.css" asp-append-version="true" />
<script type="text/javascript">
var Global = Global || {};
@{
var basePath = Url.Content("~");
}
Global.basePath = '@basePath';
</script>
</head>
This is all so that I can generate the correct URL in AJAX requests like so:
Admin.UpdateMainProductImage = function (imageId, isMain) {
$.ajax({
url: Global.basePath + 'Admin/Product/UpdateImageIsMain',
type: "GET",
dataType: 'html',//if returning view, must be html, else use json
data: { imageId: imageId, isMain: isMain },
cache: false,
success: function (data) {
alert('Main Product Image updated successfully.');
},
error: function (xhr, status, error) {
alert('Error updating image settings');
},
complete: function (data) {
//document.getElementById("quote_form").reset();
}
});
}