How to edit the web.config in application hosted on IIS - asp.net

I want to change the appsettings value in web.config. When i run the application in the vs development server, everything works fine but when i upload the application on iis it throws the foolowing exception.
[1]: http://i.stack.imgur.com/CCnsn.png
Here is what i have written in the button click event
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
config.AppSettings.Settings["FirstName"].Value = "Nitin";
config.AppSettings.Settings["LastName"].Value = "Singh";
config.Save();
While in my web.config i have created the appsettings as follows
<appSettings>
<add key="FirstName" value="Sikandar" />
<add key="LastName" value="Bharti" />
<add key="Adress" value="Lucknow" />
</appSettings>
It seems to me that this is the problem of iis security. Any help will be really appreciated.
Thanks

Yes, it is because the user your site is running as under Windows permissions can't write to that directory. (I would keep things that way.)
Perhaps you could re-write the code which saves to web.config to save to an xml file in the app_data folder or use cookies or add support for a database.

Related

How will be visual studio deployment scnerio

I consumed about deployment for my projects. For example I am developing a asp.net web api or asp.net mvc project on visual studio. I use local database and local file usernames and password for ssl. I finish the development and publis it on my local iis server and see the working application. After local iis test, I load it to server but sometimes forget the change web.config settings, connectionstrings, ssl certificate paths and password. So the working server application that users accessed fails. How can I solve this problems?
What I do is I replace the configuration in the Web.Release.config using Transform
So if for example in my Web.config I have the following connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\sample.mdf;Initial Catalog=sample;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
In my Web.Replace.config I update the connectionString for production like this:
<connectionStrings xdt:Transform="Replace">
<add name="DefaultConnection" connectionString="SERVER_CONNECTION" providerName="System.Data.SqlClient" />
</connectionStrings>
Though keep in mind that the Transformation that I did in Web.Replace.config only applies when you publish your web app. You won't be able to test it locally by just running your application in Debug/Release mode
You should implement Web.Config transformation. Check this MSDN link for more information.
Just to give an overview - by implementing Web.config transformation, your project will have different Web.Config file (For example, Web.config, Web.Live.config, Web.PreProd.Config, etc), i.e., each different file for diffrent build configurations. The main web.config should remain as it is. Its just the other config files where you will need to specify the configurations which you want to overwrite. Once done, if you publish with a particular build configuration, then the newly generated web.config (in publish folder) should have all configurations from web.config + the differences you would have specified in the transformed file for that build configuration.

EPIserver no CSS for the UI Admin section

After moving a EPI6 site to my local machine and reconfiguring it for IIS7.5 (instead of IIS6) i have a problem.
The UI Admin/Edit backend has no CSS. I suspected this was due to the virtual path mapping and i found that they where all mapped to %ProgramFiles% but on my local machine EPI is installed on %ProgramFiles(86)%. So i changed it and made sure all physical paths worked. They did.
So i felt smart and expected the CSS to load properly but no change happened.
I have tried ctrl F5 to see if its a caching problem, i have restarted the IISExpress. But still no CSS.
Any tips on something i might have forgotten?
Check using Firebug Net-tab or equivalent F12 web browser tool to see exactly which paths don't respond correctly.
Check permissions on disk for the Program Files directories in questions.
Compare Web.config to a default EPiServer IIS7 web.config to see that you have all handlers in the correct place.
I solved this.
It was not a problem of rights, but rather a configuration error. When uppgrading fom using IIS6 to IIS7.5 i forgot changing in the Web.Config:
IIS6 version
<location path="App_Themes/Default" />
to:
IIS7.5 version
<location path="App_Themes/Default">
<system.webServer>
<handlers>
<clear />
<add name="wildcard" path="*" verb="*" type="EPiServer.Web.StaticFileHandler, EPiServer" />
</handlers>
</system.webServer>
</location>

How to set Allow Parent Paths in IIS Express Config using asp directive

This is a continuation of a answered question in the following link:
How to set Allow Parent Paths in IIS Express Config
I am running a classic asp web site in IISexpress.
The same web site is also used in IIS 7.5 in a w7 pro and works fine enabling the parent paths in the IIS configuration of the web but trying to use the web.config in IISExpress does not work
The problem that I am experiencing is that in the suggested web.config in order to enable the parent paths:
<configuration>
<system.webServer>
<asp
enableParentPaths="true"
bufferingOn="true"
errorsToNTLog="true"
appAllowDebugging="true"
appAllowClientDebug="true"
scriptErrorSentToBrowser="true">
<session allowSessionState="true" />
<cache diskTemplateCacheDirectory="%TEMP%\iisexpress\ASP Compiled Templates" />
<limits />
</asp>
</system.webServer>
</configuration>
I get a 500 error every time that I try to use any asp tag
If I remove the asp tag it all works fine,
Anybody has a clue?
TIA
You have not read carefully. The configuration file that mentioned isn't web.config. It is applicationHost.config. As a shotcut:
Press Win+R
type
notepad "%userprofile%\Documents\IISExpress\config\applicationhost.config"
hit ⏎. This will open the host configuration file with notepad. You need to find your web site's section. It's something like <location path="YourSiteName"> ... </location>. Look inside that node, find the <asp> section and change with yours. Finally, don't forget to remove the asp section from web.config.

Custom HttpHandler never running

Forgive me if this is basic. I've never made one before and can't seem to figure out why it's not working. I wrote a little handler to do some parsing on CSS files. I added this:
<system.web>
<httpHandlers>
<remove verb="*" path="*.css"/>
<add verb="*" path="*.css"
type="MyNameSpace.CssRelativePathHandler,CssRelativePathHandler" />
</httpHandlers>
</system.web>
Nothing ever happens. CSS files get parsed normally. No errors, nothing, the code never runs. What am I missing? Shouldn't this cause the handler to be used when *.css files are served? (I added the "remove" later, since I thought perhaps I needed to do that to override a built-in hander, again, no difference either way).
This is IIS 6. I added the IIS 7 code anyway (after searching for answers) but makes no difference.
<system.webServer>
<handlers>
<add name="CssHandler" verb="*" path="*.css"
type="MyNameSpace.CssRelativePathHandler,CssRelativePathHandler" />
</handlers>
</system.webServer>
You need to configure IIS6 to send requests for .css files to ASP.Net.
Had you been using IIS7, your <system.webServer> element would have done that for you, but IIS6 predates this.
For IIS 6 you need to have to tell it to send *.css files to ASP.NET.
Launch IIS Manager
Right-click on Default Web Site
Click on the Home Directory tab
Under Application Settings click on Configuration...
Add a new association for .css and map it to .NET executable:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
It sounds like you need to configure IIS to enable ASP.NET to execute the .css extension.
Phil Haack has a walkthrough on doing that (just replace .mvc with .css under the heading "Mapping .mvc to ASP.NET"):
http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx
Or you can set up a wildcard mapping in IIS 6:
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/5c5ae5e0-f4f9-44b0-a743-f4c3a5ff68ec.mspx?mfr=true
I'd recommend going with the first method as doing the wildcard approach will send all requests to ASP.NET - so it has a more overhead.
PS: Further down Phil's post, he also lists "IIS6 Extension-less URL" and also covers the wildcard mapping method.

Setting variables in web config for web service consumption

I did a couple google searches about this and am not finding anything, so I thought I'd ask here.
I'm working on our internal CMS and I noticed that we're getting live data back when doing debugging because of our web services instead of the dev data that I wanted. It doesn't do this on our dev CMS website, but we're trying to do all our development on localhost. Is there any way to set up an environment variable in our web config for the URL so that the CMS points to the dev database instead of live database that is referenced in the wsdl files?
You can use the appSettings portion of the web config to for configuration information.
In the configuration section of the Web.config you will find the appSettings section:
<appSettings>
<add key="Key" value="Some Value"/>
</appSettings>
In code you can read in the value like this:
var someValue = ConfigurationManager.AppSettings["Key"];
+1 for Dan's method of storing the URL. To use this URL at runtime just update the URL property of your web service proxy object with the value from your web.config.
MyClientClass o = new MyClientClass();
o.Url = varFromWebConfig;
o.MyWebMethod();
Actually, one of my coworkers suggested an alternate way of solving this issue which seems even better to me: fixing it server-side, rather than client side like I've been trying and has been suggested here. His suggestion was to create a subdomain in IIS on all of our servers that points to the web service folder and then put host files for the appropriate web server on my local machine. This seems like the ideal solution to me since it wouldn't require changing all the current web service proxy objects like the client side solution would, just the web service consumption within App_WebReferences.
YES!!! USE Web.config transforms
Web.config contains the configuration that will run in your IDE while debugging:
<configuration>
<appSettings>
<add key="Service.Name" value="http://debugserverURI/Service.asmx"/>
</appSettings>
</configuration>
On publish in "Release" mode, transforms in Web.Release.config will be applied:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<!--point to production server -->
<add key="Service.Name" value="http://PRODUCTIONserverURI/Service.asmx"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
</configuration>
You can do the same for Web.[whatever_build_you_want].config, if you support both test and prod servers.

Resources