Accessing .net Web.Config in AngularJS project - c#

I have an AngularJS project in "empty" type .net project, which is hosted on IIS with .Net 4.0 framework. I basically don't use any .net dlls but since the project has a web.config file i add some App setting to use in the project. Trying to set development/production setting.
Is there a way to access AppSettings in the .net web.config file from AngularJS, because i am not using a MVC project, I can't use razor (#System.Configuration.ConfigurationManager.AppSettings["url"]).
Is there another way to this?
Thanks

You don't.
The Web.Config file is a server-side configuration file, AngularJS is a client-side framework.
You can't access it from the browser, because IIS will never - for security reasons - serve it to clients.

You may want to expose a WebApi controller or MVC controller that returns JSON that represents web.config file... or at least the JSON object that has the data in the web.config file you need to send to client. The WebApi controller would read from web.config via configuration manager, convert to JSON, and send to client.
I am doing something similar where I am storing my service endpoints that my angular app needs in my web.config, but I want to transform the .config files based upon debug, dev, staging, production etc.

Related

where to add folder path in asp.net core instead of harcoding it?

I have a list of folder locations in my application.
How do I add folder path location in the app without hardcoding it.
I found web.config to use appsettings but I cannot load it in my Visual studio
and what is the different between appsetting.config and web config
appsettings.config has replaced web.config in ASP.NET Core.
appsettings.config is where you should but and confirmation information for your application.
Data in appsettings.config can be accessed in the program via the IConfiguration interface, which is available as a property in the Startup class, and can be accessed in controllers etc. through dependency injection.
Full details here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1

How to read site's web.config file from Azure WebJob?

I am using custom web.config configuration elements to configure complex application services for my ASP.NET MVC application. I need this configuration data in my Azure WebJob which is deployed alongside the web app. I am trying to avoid copying these sections from website's web.config to webjob's app.config file.
So, apart from connection strings and application settings, is there a way for an Azure WebJob to read the website's web.config file?
Ideally, I would like to use ConfigurationManager.GetSection to create my custom ConfigurationSection instances.
Most web apps + web jobs have sensitive settings that you don’t want in a config file. You can deploy those secrets using ARM/PS see deploying secrets to Azure. So just add all your settings to your ARM/PS script.
Azure App settings apply to the web job and the web app, this is the recommended approach.
Here is a solution for a build time. Probably this sample Msbuild script would insert a peeked xml elements from your web.config into another xml file. Please note it is not tested.
<ItemGroup>
<PublishPackages >
<WebConfigXml>
$(YourProjectPath)web.config
</WebConfigXml>
<WebJobConfig>
$(YourWebJobPath)app.config
</WebJobConfig>
</PublishPackages>
</ItemGroup>
<Target Name="InjectConfigs">
<XmlPeek
XmlInputPath="%(PublishPackages.WebConfigXml)"
Query="//parent/someelement">
<Output TaskParameter="Result" ItemName="Peeked" />
</XmlPeek>
<XmlPoke
XmlInputPath="%(PublishPackages.WebJobConfig)"
Query="//parent/someelement"
Value="$(Peeked)"
Condition =" '$(Peeked)'!=''"
/>
</Target>
</Project>
These docs can help: Configuring Parameters for Web Package Deployment, XmlPeek task
And if XmlPoke and XmlPeek won't do the thing another way is to use XmlFile task. Hope it helps!
UPDATE. Changed to use only XmlPeek and XmlPook without XmlFile.
Here is a solution for installation time. There is a technology called Web Deploy that allows to create packages which can be run from command line and install services. And it allows parameterization of .config files by using one xml file called Parameters.xml. This one can be shared by your web site and Web Job I believe. The howto manual on doing it.
One approach we used for a similar purpose to this was to put all the common configurations on a Table Storage account in Azure and built a helper class that reads the configuration from what we referred to as: "Run Time Configuration", which is basically a configuration database that is hosted in Azure Table storage and all components could read that information...
Try using the CloudConfigurationManager class in the Microsoft.WindowsAzure.ConfigurationManager NuGet package.
ie.
var val = CloudConfigurationManager.GetSetting(key);
I find that this seems to pull the settings from the host Web App while running on Azure. If you're debugging locally you'll still need to set the App.config values, I'm afraid.
Edit: Just re-read your question again (d'uh!) and saw your comment about using Sections. AFAIK that's not supported by this class - AppSettings section only unfortunately.

Web site references a console app which uses NHibernate to talk to database

I've written a console app which basically downloads an XML file, manipulates it and puts the data into a database. This app uses NHibernate and MindScapes NHibernate designer tool. Which works fine when I run it as a stand alone thing.
Now I want to create an MVC website and when a certain button is clicked on that website, I want it to trigger the console app to download the appropriate xml file and do its thing.
I get an error:
Could not find file
'C:\Dev\WagerWatcher\WagerWatcherWeb\bin\hibernate.cfg.xml'.":"C:\Dev\WagerWatcher\WagerWatcherWeb\bin\hibernate.cfg.xml
WagerWatcherWeb is the website, So I think it's looking in the WagerWatcherWeb project for a file which is actually located in the 'WagerWatcher' project.
Is it as simple as changing an app.config file somewhere to redirect to the actual file? and if so, where abouts? I can't fins it to save myself,
Just place (copy) the configuration file (used in your console app) to deployed web-app folder. Read: 3.8. XML Configuration File
An alternative approach is to specify a full configuration in a file named hibernate.cfg.xml. This file can be used as a replacement for the <nhibernate> or <hibernate-configuration> sections of the application configuration file.
While the web application is the "run-time" (just calling the API of your console application) it must be provided with all configuration. Even for referenced libraries...

How do I reference a WSDL file with a relative path?

I have a WSDL file on disk and loaded as a web reference. The problem is that the web reference itself is looking for the WSDL on my machine (using an absolute path), which obviously won't work on teammates' machines.
Is it possible to have the web reference look for the WSDL with a relative path, or from the Resources file? The WSDL is for a third-party service that we've modified to add some extra fields (per their recommendation).
When you add a service reference to the WSDL file on your machine, it gets copied into your "Service References" folder in your project. At that point, the API for your service is determined. There is a "Reference.svcmap" (XML) that is created that holds the location of the original location of the WSDL file. This is so that you can update the service reference in your project if the API changes. But, even without having that original WSDL file you can check in the code and your team can use it without issue. But, if you want them to be able to update the service if the API changes, then you need to actually host that WSDL file in a publicly accessible place. Usually, the web service itself provides the WSDL file and you wouldn't have it on your file system. If you plan to update and change the web service, you should connect to the WSDL hosted on a development web server. That's really where it belongs, and not a local file. But, there's no reason your local file reference wouldn't work just fine until someone tried to update it. If your web service never changes, you've got no problem. If it could change, then host it somewhere and reference a real URL.
My initial testing of tweaking the XML in Reference.svcmap file to try for relative path references did not work, but if you decide you really want to get this to work as your question describes, this is the place to make your attempt.
My suggestion would be to create a programmatic proxy using the cmd line tool wsdl.exe or svcutil.exe to create the .cs or .vb proxy that you could then add to the solution.
This allows for not needing the wsdl file at all. See more information on how here for .NET 2.0 http://msdn.microsoft.com/en-us/library/7h3ystb6(v=vs.80).aspx
for .NET 3.5 or later and svcutil look here: http://msdn.microsoft.com/en-us/library/aa751905.aspx

Reference Web.Config file from another project in same solution C#

I have a VC2010 C# Solution, with a number of projects in it.
So for example, I have a web project, and I have a class library.
In the web.config file, I have a key in the <appSettings> section, e.g.
<add key="FileDirectory" value="G:\ftproot\sales" />
I have also added a key in the Web.Production.config file to reflect the file directory on the server.
So when I reference it in my web project (It's MVC) - I do so like this:
var FTPPath = ConfigurationManager.AppSettings["FileDirectory"];
this works fine within my web project. However, I also need to reference this in the class library, which gets to my question - Is there a way to reference a key in the web.config file from another project, e.g. a class library, in the same solution??
All help is appreciated.
Thanks
Yes you can use exactly the same code. .Net will look up the configuration key in the config file of the application which started the app domain. A class library used by such an application will have access to it's config file.
class libraries do not have their own configuration. They use the configuration of which ever executable they are being used in.
This means that for you you should be able to use the same code, and it will read the setting from the config (assuming that it is there).
This is not always convenient though (for example if you write a .net based plugin for a MMC snap-in, as this means you have to modify the mmc.exe.config in the system folder.)
You might be better having a method to pass this required configuration setting into you library code. then in apps where you control the config you can just read it from there and pass it in, and in apps where you can't you can use another approach, like reading from the registry or from a manually read config file. Or have the best of both worlds and make it so you can pass it in, and if this is not done it attempts to read it from the default configuration.
This question has some more details on the pitfalls associated with dll configuration, but also has some techniques for doing it if you need to.

Categories

Resources