URLCompression + Response Filter Conflict - asp.net-2.0

I have IIS 7.5 with URL compression enabled for dynamic content. I wanted to add a response filter to remove modify the rendered html and for some reason I kept getting garbage data while filtering.
The code for the response filter's write method is below:
Encoding encoding = HttpContext.Current.Response.ContentEncoding;
string html = encoding.GetString(buffer);
html = regFindFollow.Replace(html, new MatchEvaluator(AddFollowNoFollowAttribute));
byte[] outdata = encoding.GetBytes(html);
This starts to work when I remove URL compression from web config. Am I missing something here? Is there an order for response filters that can be specified?
Config I am using is
<urlCompression doDynamicCompression="true" dynamicCompressionBeforeCache="true" />

Changing the config with
<urlCompression doDynamicCompression="true" dynamicCompressionBeforeCache="false" />
Fixed this. I suppose during the execution module received compressed html and couldn't parse it.

Related

ASP.NET - Determine max file upload size at runtime

I have several places on my site where I have helptext telling users what the maximum allowed file upload size is. I would like to be able to have this be dynamic, so that if I change the request limits in the web.config file, that I don't have to go and change the form instructions in a bunch of places. Is this possible using ConfigurationManager or something?
Since you didn't give any further Details: As pointed out here, you have 2 options to set a size Limit for your whole Application.
Depending on that you need to approach this a little differently:
If you use <httpRuntime maxRequestLength="" /> you can get the Info through WebConfigurationManager
//The null in OpenWebConfiguration(null) specifies that the standard web.config should be opened
System.Configuration.Configuration root = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
var httpRuntime = root.GetSection("system.web/httpRuntime") as System.Web.Configuration.HttpRuntimeSection;
int maxRequestLength = httpRuntime.MaxRequestLength;
In Priciple you should be able to do the same with <requestLimits maxAllowedContentLength="" />. But the system.webServer-Section in WebConfigurationManager is declared as IgnoreSection and can't be accessed. It may be possible to change this behaviour in application.config or similiar of IIS. But since (in my case) even the .SectionInformation.GetRawXml() failed, I tend to declare this a lost case.
My Solution in this case would be to access the Web.config-File manually:
var webConfigFilePath = String.Format(#"{0}Web.config", HostingEnvironment.MapPath("~"));
XDocument xml = XDocument.Load(System.IO.File.OpenRead(webConfigFilePath));
string maxAllowedContentLength = xml.Root
.Elements("system.webServer").First()
.Elements("security").First()
.Elements("requestFiltering").First()
.Elements("requestLimits").First()
.Attributes("maxAllowedContentLength").First().Value;
Another Solution to this is proposed by #Roman here using Microsoft.Web.Administration.ServerManager for which you need the Microsoft.Web.Administration Package

HTTPTargetConnection - why proxy.pathsuffix gets appended to URL

I have created multiple target-end points based on my back-end servers. Each target end point has a fixed URL.
URL is configured as -
<HTTPTargetConnection>
<URL>https://example.com/test/</URL>
</HTTPTargetConnection>
I noticed that "proxy.pathsuffix" is automatically getting added to url (https://example.com/test/). How can I avoid this?
A bit outdated in reply - but, Assign Message works for this (to avoid JavaScript):
<AssignVariable>
<Name>target.copy.pathsuffix</Name>
<Value>false</Value>
</AssignVariable>
You'll need to create a new JavaScript callout policy in your proxy request flow. In this JavaScript callout, simply add a single line:
context.setVariable("target.copy.pathsuffix", false);

How to read system value from web.config and use in ASP.NET MVC C# method

I'm working on a ASP.NET MVC3 web application (not written by me) which has a max upload size of 100MB. Now this web application gets installed on server machines for customers so it would be nice if this value max upload size was configurable for each customer. They have access to edit the web.config for the web application if need be.
Now there's a value in the web.config like so:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="104857600" />
</requestFiltering>
</security>
</system.webServer>
Also there's another value here which appears to be similar:
<system.web>
<httpRuntime maxRequestLength="104857600" executionTimeout="360" />
</system.web>
That 104857600 bytes appears to be the 100MB file upload limit. However on changing the value I discovered that this isn't the authoritative value and it wasn't obeying the new limit. So after some more digging I found somewhere else in the C# code was a hardcoded value public const double MaxContentSize = 104857600 and another C# method was using that value to accept/deny the Ajax file upload.
So what I think I'd like to do is replace that hard coded number in the code so it reads from the value in the web.config. Then at least anyone can change that value in the web.config when they deploy the website.
Can you do something like this?
MaxContentSize = ConfigurationManager.systemWeb.httpRuntime['maxRequestLength'];
I've seen some examples using appSettings in the web.config e.g.
<appSettings><add key="MySetting" value="104857600" /></appSettings>
then accessing it like:
ConfigurationManager.AppSettings["MySetting"]
But that would mean adding a custom value in there and now we'd have 3 places to change it in the web.config. Anyone got an idea how to do it properly?
Many thanks
You can do something like:
int maxRequestLength = 0;
HttpRuntimeSection section =
ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
if (section != null)
maxRequestLength = section.MaxRequestLength;
There seems to be no easy way to read the system.webServer section, because it is marked as "ignored" from machine.config.
One way is to parse the XML of the web.config file directly:
var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
var section = config.GetSection("system.webServer");
var xml = section.SectionInformation.GetRawXml();
var doc = XDocument.Parse(xml);
var element = doc.Root.Element("security").Element("requestFiltering").Element("requestLimits");
string value = element.Attribute("maxAllowedContentLength").Value;
Try:
var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/")
var section = (System.Web.Configuration.SystemWebSectionGroup)config.GetSectionGroup("system.web")
var maxRequestLength = section.HttpRuntime.MaxRequestLength

ASP.NET: HttpRequest.Url truncates trailing '.' characters

If the URL that arrives to ASP.NET application contains trailing full stops - '.', they are truncated from the Url property in HttpRequest.
For example if the URL is "http://server/folder.../", the following call:
HttpContext.Current.Request.Url.PathAndQuery;
returns "/folder/" instead of "/folder.../".
Tried this solution, but it helps only if the Uri is constructed after the suggested code executes, while HttpRequest is probably constructed before any code in ASP.NET web application is executed.
Any ideas how to preserve trailing '.' in HttpRequest.Url?
You can add the relaxedUrlToFileSystemMapping to your web.config inside <system.web> section.
<httpRuntime relaxedUrlToFileSystemMapping="true" />
This will preserve the dots in the url.
But for some reason Url.PathAndQuery wont contain the dots, while RawUrl contains them.
HttpContext.Current.Request.Request.RawUrl;
Keep in mind that there are probably some security implications when enabling relaxedUrlToFileSystemMapping.

Encoding Issue ASP.net

i upload a file with ASP.net which Contains an "ä" or "ü", when uploaded, on the server the "ä" or "ü" is replaced with another special character. How can i solve this issue. Same Problem is with normal textboxes, so i guess it has to do something with Encoding.
Maybe u have got a solution or an idea, would be quite nice...:-)
Most likely an encoding issue.
You could check:
Whether the encoding meta tag on the HTML page is correct.
Whether the pages are sending the correct encoding to the client (in the HTTP header)
Whether the pages are actually encoded in the correct encoding (via VS.NET "File" menu, menu item "Advanced Save Options").
To see the HTTP headers, use e.g. ieHttpHeaders extension for Internet Explorer.
To change the sent encoding, use either the <globalization> tag in WEB.CONFIG to change for all pages or use the #Page directive to define the response encoding on a per-page-basis.
put following code in web.config
<configuration>
<system.web>
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
/>
</system.web>
</configuration>
if(File.Exists(Server.MapPath("../App_Data/Karten/") + FileUpload1.PostedFile.FileName.Replace("ö","oe").Replace("Ö","Oe").Replace("Ö","ae").Replace("ä","Ae").Replace("ü","ue").Replace("Ü","Ue"))){
Label1.Text = "Datei existiert bereits";
}else{
string filepath = FileUpload1.PostedFile.FileName;
System.Diagnostics.Debug.WriteLine("Filename" + filepath);
System.Diagnostics.Debug.WriteLine("Filename" + filepath.Replace("ö","oe").Replace("Ö","Oe").Replace("Ö","ae").Replace("ä","Ae").Replace("ü","ue").Replace("Ü","Ue"));
if (FileUpload1.PostedFile.FileName.ToLower().EndsWith("jpeg") || FileUpload1.PostedFile.FileName.ToLower().EndsWith("jpg"))
{
System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
if (UploadedImage == null)
{
Label1.Text = "Kein Bild";
System.IO.File.Delete(Server.MapPath("../App_Data/Karten/") + filepath);
}

Resources