TransWikia.com

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

Stack Overflow Asked by Mathew Alden on November 4, 2021

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. That is, my app is structured like this:

+ Solution
| - Project
|   + Properties
|   + AngularApp
|   | + dist
|   | + e2e
|   | + src
|   | - (etc)
|   + App_Start
|   + Model
|   + Global.asax
|   - Web.config
- ProjectTest

I have some URL rewrite rules so that any request website.com/x that doesn’t refer to one of my Controllers will instead be redirected to website.com/AngularApp/dist/AngularApp/x. Everything I’ve described so far 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). Three different portions of my app need to know that this new subdirectory should be considered root – the Angular app needs to have this configured as root, the rewrite rules need to incorporate this, and a certain pieces of C# code also needs to know about this new root (I’ll save you the details here). Currently, I’ve had to specify this subdirectory in both the Pre Build Events (which are building the Angular app) and the Web.config (which controls my rewrite rules) and the Settings (which are accessed by my aforementioned C# code). It’d be better if I could have a single configuration that they all pulled from. My above question would at least allow the combination of two of these disparate three configurations.

One Answer

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.

Answered by timur on November 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP