Can I create a virtual directory in web.config? - asp.net

I have read that the applicationhost.config file sets the defaults and all directories below root inherit those settings.
I know its possible to change the defaultdirectory in a web.config, but I can't seem to do the same with a virtualdirectory.
Any pointers, help is appreciated.
Note: WEB.CONFIG file below DOES NOT work... just there to explain what I want to.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<customErrors mode="Off" />
<compilation debug="false" targetFramework="4.0" />
</system.web>
<system.webServer>
<httpErrors errorMode="Detailed" existingResponse="Auto"/>
<asp scriptErrorSentToBrowser="true"/>
<directoryBrowse enabled="false" />
<defaultDocument>
<files>
<clear />
<add value="Default.aspx" />
<add value="default.html" />
</files>
</defaultDocument>
<httpProtocol>
<customHeaders>
<clear />
</customHeaders>
</httpProtocol>
</system.webServer>
<system.applicationHost>
<sites>
<site name="wed" id="1" serverAutoStart="true">
<application path="/alpha-beta">
<virtualDirectory path="/" physicalPath="\url.com\wwwroot\alpha\beta" />
</application>
</site>
<applicationDefaults applicationPool="DefaultAppPool" />
<virtualDirectoryDefaults allowSubDirConfig="true" />
</sites>
</system.applicationHost>
</configuration>

Related

how to remove cookies from my website

I developed a website with Asp.net Website project, and recently I added some https protocol on it. my problem is that the website shows that it is saving cookies, while I really do not like to save any cookies from my users.
I am not familiar with cookies very much but I am sure I did not put any code to save user cookies, how I can be sure If there would not be any cookies for my users and delete any settings for this and why this happening automatically?
my project is a very simple single page website with no form and authentication
here is my web.config if needed.
<?xml version="1.0"?>
<configuration>
<system.web>
<httpRuntime enableVersionHeader="false" targetFramework="4.5" />
<compilation debug="true" targetFramework="4.5"/>
<customErrors mode="On" />
<sessionState mode="Off" />
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<clear />
<remove name="X-Powered-By" />
</customHeaders>
</httpProtocol>
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files\(compressionType)\(AppPool)\(WebSite)\compressed files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/json" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/atom+xml" enabled="true" />
<add mimeType="application/xaml+xml" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true" dynamicCompressionBeforeCache="true"/>
</system.webServer>
</configuration>
Appreciate very much
ASP.Net has by default session state (https://msdn.microsoft.com/en-us/library/ms178581.aspx) turned on, which in turn relies on cookies by default. If you don't need session state (because users don't logon etc) you can turn it off in the web.config file. Just add
<sessionState mode="Off">
under the
<system.web>
element.

Config file XDT transformation InsertIfMissing is skipping children

I am having trouble doing config transform, adding app settings on nuget package install where element appSetting may or may not exist.
What I want to happen:
appSetting element does not exist
Insert appSetting element and its children
appSetting element exist
Insert children if missing
I only get one or the other to work, but not both occasions.
web.config.install.xdt
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings xdt:Transform="InsertIfMissing" >
<add key="Swagger.Contact.Name" value="Some Name" xdt:Transform="InsertIfMissing" />
<add key="Swagger.Contact.Email" value="some#email.address" xdt:Transform="InsertIfMissing" />
</appSettings>
</configuration>
Example 1
web.config BEFORE
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
<customErrors mode="Off" />
</system.web>
</configuration>
appSettings element not present before transformation.
web.config AFTER
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
<customErrors mode="Off" />
</system.web>
<appSettings>
<add key="Swagger.Contact.Name" value="Some Name" />
<add key="Swagger.Contact.Email" value="some#email.address" />
</appSettings>
</configuration>
Example 2
web.config BEFORE
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
<customErrors mode="Off" />
</system.web>
<appSettings>
<add key="Other.Key" value="With Some Value" />
</appSettings>
</configuration>
appSettings element present before transformation.
web.config AFTER
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
<customErrors mode="Off" />
</system.web>
<appSettings>
<add key="Other.Key" value="With Some Value" />
</appSettings>
</configuration>
Nothing happens in example 2 as the appSettings element already exist, I would like it to still evaluate its child elements and insert those if they do not exist, but it seems they are just ignored. Is there any other value for the attribute xdt:Transform I can use, or any other hacks to work around this issue?
I had a similar problem quite a while back. The workaround I applied was to have two entries with <appSettings> in the XDT file, one to check if its absent and if yes, then go ahead and insert it. The other was for the scenario when the <appSettings> element was already present. Here is short snippet to help you with your problem:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings xdt:Transform="InsertIfMissing">
</appSettings>
<appSettings>
<add key="Swagger.Contact.Name" value="Some Name" xdt:Transform="InsertIfMissing" />
<add key="Swagger.Contact.Email" value="some#email.address" xdt:Transform="InsertIfMissing" />
</appSettings>
</configuration>
Let me know if this works for you.

How to set encoding to utf-8 on a IIS web.config file?

I have this
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
<add key="ContentServer" value="true" />
</appSettings>
<connectionStrings />
<system.web>
<trace enabled="false" requestLimit="25" pageOutput="false" traceMode="SortByTime" localOnly="false" />
<customErrors mode="Off" />
<compilation debug="false" />
<authentication mode="Windows" />
</system.web>
</configuration>
How do I set the encoding to utf-8 for files sent to client. Actually There can be other types of files sent to clients like js,css,images. But I have aspx pages, that I want to set text/html; charset=utf-8" content-type to. How can I add this to this web.config code?
Thanks
You need to set the responseEncoding attribute of the globalization element.
<globalization
responseEncoding="utf-8"
/>
In context:
<configuration>
<system.web>
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
/>
</system.web>
</configuration>

How to encrypt a file linked to a web.config

I have a web.config file that links to a common.config file. common.config is being used by multiple applications. I used the aspnet_regiis.exe, but that only encrypts the web.config file. How can i encrypt the common.config file?
web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings file="C:\Users\naem\Documents\common.config" />
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
common.config file:
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="myKey" value="This is the Value!!!!"/>
</appSettings>
I don't have the rep to comment on the original post, but this looks like a duplicate of How to encrypt .config file Asp.net
I found workaround to solve this issue. Hope it helps.
Rename your common.config to web.config temporarily.
Add configuration as root element to this file. So your common.config will look like as follows.
<configuration>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="myKey" value="This is the Value!!!!"/>
</appSettings>
</configuration>
Run encrypt command.
aspnet_regiis.exe -pef appSettings "C:\Users\naem\Documents"
Open encrypted file and remove Configuration tags.
Rename file to Common.config

HTTP Post filters out Content-Type "text/xml"

My page is written in ASP.Net, and our site runs IIS 7.5.
I'm trying to receive an Xml document from an outside source posting to a page in our website. I can send a test document from a web page and receive it, using Content-Type “application/x-www-form-urlencoded”. The sender's documents are Content-Type "text/xml." I have modified the web.config to try various things, all of which still give us a "blank" document from the sender. Is there some setting that I"m still missing that would cause this?
My web.config settings are:
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
<sessionState mode="Off" cookieless="true" />
<httpRuntime requestValidationMode="2.0" enableVersionHeader="false" />
<webServices>
<protocols>
<add name="HttpGet" />
<add name="HttpPost" />
</protocols>
</webServices>
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="P3P" value="policyref='https://oursite/w3c/p3p.xml',CP='NON DSP COR CUR OUR IND PUR ONL PHY'" />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="POST,GET" />
<add name="Access-Control-Allow-Headers" value="content-type:text/xml;charset=utf-8"/>
</customHeaders>
</httpProtocol>
<staticContent>
<mimeMap fileExtension=".aspx" mimeType="text/xml" />
<mimeMap fileExtension=".aspx" mimeType="application/x-www-form-urlencoded"/>
</staticContent>
<httpErrors errorMode="Detailed" />
<httpCompression>
<staticTypes>
<add mimeType="text/xml" enabled="true"/>
</staticTypes>
<dynamicTypes>
<add mimeType="text/xml" enabled="true"/>
</dynamicTypes>
</httpCompression>
<security>
<requestFiltering>
<fileExtensions allowUnlisted="true" />
</requestFiltering>
</security>
</system.webServer>

Resources