ASP.NET parameter/variable in both app Settings and app Build Events

In ASP.NET, we have Application Settings, and we have Application Pre/Post Build Events. Is it possible to access a Setting from the Pre Build Event? Or is it possible to inject the value of a Setting from the Pre Build Event?

Full context:

I have an Angular app embedded in an ASP.NET 4 Web API app. Everything is working great. The trouble is, this app is not being deployed to the root of the domain; it’s being deployed to a subdirectory of the domain. (i.e. website.com/app instead of website.com) Both the Angular frontend and the ASP.NET backend need to be configured to know that they’re being served out of a subdirectory (otherwise the way they’re constructing relative URLs doesn’t work; I’ve tried). Currently, I’ve had to specify this subdirectory in both the Pre Build Events (which are building the Angular app) and the Settings (which are accessed by ASP.NET). It’d be better if I could have a single configuration that both pulled from. Hence my above question.

Add Comment
1 Answer(s)

My suggestion is not to do with any build events, it’s rather to bootstrap your angular app with some values from hosting ASP view. If I understand your app layout correctly, you might be able to push some variables onto a Razor view as plain javascript that would get picked up by Angular upon startup.

Model

public class SampleViewModel {     public string Path { get; set; } } 

Controller

public class HomeController : Controller {     [HttpGet]     public ActionResult Index()     {         return View(new SampleViewModel() { Path = "/app/1/test" });     } } 

View

@model HelloWorldMvcApp.SampleViewModel @{     Layout = null; }  <!DOCTYPE html> <html lang="en"> <head>     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.min.js"></script> </head>      <body>     <h1>Server-side path @Model.Path</h1>     <hr/>     <div ng-app="myApp">         <div ng-controller="MyController">             <h1>{{message}}</h1>         </div>     </div>     <script type="text/javascript">         var clientSidePath = "@Model.Path"; @* Razor happily renders values into javascript blocks *@         var app = angular.module('myApp', []);         app.controller('MyController', function($scope) {             $scope.message = 'Client-side path ' + clientSidePath;         });     </script> </body> </html> 

The absolute minimum Dotnet fiddle here: https://dotnetfiddle.net/pO2wHN. For simplicity sake I opted for AngularJS (which is probably not the exact version you use) but similar approach should work with recent Angular as well.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.