I have a Web application in .net Core 2.1 using signalR. I need to pass HubUrl into custom javascript file. Is that possible in .net Core?
Sample of js code:
const connection = new signalR.HubConnectionBuilder()
.withUrl('http://localhost:5000/hub') //Here I need to read appSettings.json to get value from there
.configureLogging(signalR.LogLevel.Information)
.build();
appsettings.json located on server. So you need to add end-point to controller that returns needed value.
Controller:
public class MyController:Controller{
private readonly IConfiguration configuration;
public MyController(IConfiguration configuration){
this.configuration = configuration;
}
[HTTPGet]
public ActionResult GetConfigurationValue(string sectionName, string paramName){
var parameterValue= configuration[$"{sectionName}:{paramName}"];
return Json(new { parameter= parameterValue});
}
}
Client side:
$.ajax({
type:"GET",
url: "/MyController/GetConfigurationValue"
data:{
sectionName = "MySection",
paramName = "MyParameter"
}
}).done(
function(parameterValue){
//do what you need
});
In appsettings.json:
{
"MySection":{
"MyParameter":"value that I want to get"
}
}
In appsettings.json
"ApiUrls": {
"commonUrl": "https://localhost:44348/api/"
}
In _Layout.cshtml
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
<script>
const commonUrl = @Json.Serialize(@Configuration.GetSection("ApiUrls").GetSection("commonUrl").Value)
</script>
now this will be like a global variable accessible to all js files
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With