ASP.NET 3.5 Page character encoding problem - asp.net

I have a problem in my asp.net 3.5 application (C#) when I try to render in my pages characters like 'è' which are shown in a very strange manner (if i'm lucky i get a ? mark in my web page). in fact Expression Web, when i open my web site, substitutes the è char with �...
How can I tell asp.net that I want to use a particular charset so that i can write in the html source letters like è without using hexadecimal codes??????
I tried in the web.config this:
inside the system.web namespace of the file but nothing works....
Can anyone tell me how to do? THANKS in advance

OK I inserted in every page the element so that everything is ok...

Maybe you can use HttpUtility.UrlEncode or Server.HTMLEn

Related

Why does Sitecore seem to mangle parentheses in the query string?

I am noticing something strange with Sitecore and parentheses in the query string. For some reason Sitecore seems to be doing something odd to the query string. Here are the steps to reproduce this behavior:
Install Sitecore 7.5
Create a simple sublayout called TestSublayout
On the TestSublayout add an ASP Literal tag called litTest
In the code behind for the sublayout, in the Page Load event add the following code: litTest.Text = Request.QueryString.ToString();
Create a simple item in Sitecore called TestItem and use the Sample Item data template
In the presentation details for TestItem, put the TestSublayout in the main placeholder.
Build the Visual Studio project and publish the site.
Open up a browser and go to http://example.com/testitem.aspx?selecteditem=surf(x)%203D%20Laminates
For some reason, Sitecore does something with the parentheses and this is what you see on the screen:
selecteditem=surf&%2340%3bx&%2341%3b+3D+Laminates
The reason I believe that Sitecore is doing something, is that I tried these exact same steps on a non-Sitecore project and it worked fine. The parentheses were not garbled or translated in to anything. They were just in the string as I would expect like this:
selecteditem=surf(x)%203D%20Laminates
Any ideas?
I found out from Sitecore that this was a known problem that was fixed in version 7.2 but had not been merged in to the 7.5 release. So they gave me a patch that fixed the problem.

Encode the html string in Asp.net MVC

<%= Html.TextArea("text", "This is <b>Zeb</b>") %>
From the above statement what i expect is to show the string in textbox as:
This is Zeb.
But what i actually get is
This is <b> Zeb </b>
My string is not properly encoded. So please can someone tell me where is the problem?
I have read it from book Professional ASP.NET MVC 4. It's on page 100.
There is no problem, it is expected behavior. Here is a same question: ASP.NET MVC3, Html.TextAreaFor without encoding?
Your question has nothing to do with encoding. You are telling MVC (and the browser) to show a text area to edit that string, and that's what it's doing.
If you want an HTML editor with which your users can edit the HTML (while seeing it properly rendered), you should check this post:
What's the best WYSIWYG editor when using the ASP.NET MVC Framework?
Basically, you'll usually have to include the .js and .css of the selected editor, and activate it on the text area that you are currently sending to the broser. That will create an editor like the one you'll find in Wordpress, for example.
Edit:
Now that you have edited your question, I think you are misunderstanding what the book was referring to. When it says that your text has been encoded it means that the < > characters have been translated into & lt; and & gt; so that the browser doesn't treat them as "open HTML tag" and "close tag".
If you look at the source HTML in your browser, you'll see that the string has been properly encoded (as & lt; and & gt;).

Strange characters output on asp.net mvc page

Sometimes (randomly) my asp.net mvc application outputs page where single character is being replaced with weird question marks, as shown on image
This behavior is constant until data changes, and it does not disappear with page refresh. The whole page is in Georgian unicode. I am using asp.net mvc 3 with visual studio 2010 and iis express. This is also observed on published site to IIS 7.5. I have seen similar issues in asp.net webforms application too.
I have observed this behavior not only on db-provided data, but simple anchors, generated with html helper
TagBuilder addAnchor = new TagBuilder("a");
addAnchor.AddCssClass("add-item");
addAnchor.MergeAttribute("href", "#");
addAnchor.SetInnerText(SharedResources.Add); //resource item contains string დამატება
which output anchor "დამატ���ბა"
And this is not issue with page rendering on browser-side, as raw response already contains these symbols.
Update:
As it seems, disabling buffer output by setting Response.BufferOutput = false; removes weird characters from page. But I use Cassette for referencing bundles, and it cannot work with disabled buffer output - no scipts or stylesheets get referenced on page.
Has anyone of you had a similar problem? This is actually critical for me, as currently it affects data where that single character being damaged is the correct answer information of a multi-select test question.
I had the same bug. It is Cassette with a feature that rewrites the result HTML content (and needs BufferOutput set to true) to move referenced script and stylesheet tags to the top if needed.
See the IsHtmlRewritingEnabled option at http://getcassette.net/documentation/configuration.
When I turned off this feature, the bug has disappeared.
My observations:
It is appeared only with remote requests. Local requests are always ok.
The location of question marks is always constant for all clients, browsers and computers.
The location of question marks changes only after recompilation of the application.
I would imagine it's to do with character sets. I've had this issue when i've tried to display a character that isn't in a particular character set.
such as ’ or – (’ or –) in UTF-8 when the character hasn't been HTML encoded previously.
I too had output like this. In my case the source of error was within Views/web.config. An assembly was listed in pages/namespaces, which was not part of the referenced assemblies

Removing the byte order mark on asp.net page

I'm really stuck. I'm working on an integration project where I'm creating a html fragment using ASP.NET. This fragment will be included as part of another site. The html fragment is created using a simple ASP.NET page with a minimal code behind file.
Te site that will include this html fragment is running PHP. The developer there is complaining that my code is sending the byte order mark as part of the html fragment and this is making his life difficult.
I'm using Visual Studio 2010 for development. I've saved the relevant aspx and aspx.cs files as UTF-8 without signature. However, this doesn't seem to have made a difference and now I'm pretty much out of ideas. The rest of the site is set to use UTF-8 as standard (defined in web.config).
Where should I be looking to alter the BOM behaviour for my aspx file?
If you save the file in Visual Studio I believe it will keep adding the BOM. You need to use a different editor to remove the marker such as NotePad++.
Actually in the advanced save as dialog in Visual Studio there is also an option to save UTF-8 without signature. Have you tried this?
It's confirmed in this question.
You can also use plain old NotePad to save with a different encoding.
Save As...Encoding DropDown.

Minify Html output of ASP.NET Application

What are the ways by which we can reduce the size of the HTML Response sent by an asp.net application?
I am using Controls which are not owned by me and it produces output with white spaces. I am interested in Minifying the entire HTML output of the page just like how google does (View source www.google.com) to improve the timing.
Is there any Utility classes available for ASP.NET which can do this stuff for me?
There is no need to do it at run time. Because it can be done at compile time.
Details: http://omari-o.blogspot.com/2009/09/aspnet-white-space-cleaning-with-no.html
Try HTTP module as described here: http://madskristensen.net/post/a-whitespace-removal-http-module-for-aspnet-20
For Microsoft .NET platform there is a library called the WebMarkupMin, which produces the minification of HTML code. For each ASP.NET framework has its own module:
ASP.NET Core - WebMarkupMin.Web. Implemented as an HTTP module, so it can work with any framework. Suitable for use in the ASP.NET Web Pages framework (Razor).
ASP.NET MVC - WebMarkupMin.Mvc
ASP.NET Web Forms - WebMarkupMin.WebForms
Documentation is available at - http://webmarkupmin.codeplex.com/documentation
I want to comment on Thorn's suggestion (but I'm new to stack overflow).
The linked code (omari-o.blogspot.com) doesn't support MVC4, and although the code is open source it cannot easily be upgraded because of braking changes between MVC3 and MVC4.
There might be whitespaces written to the http result at runtime, only the developer of the actual site can know that. Thus static minification of template files (aspx) is not foolproof at all. Dynamic minification, which is suggested by gius, should be used to guarantee that whitespaces are removed correctly, and unfortunately this will incur a runtime computation cost. If code dynamically writes spaces to the output, it will have to be removed dynamically.
The accepted answer does not work with MVC 4, so here is a similar lib that minifies at build-time https://github.com/jitbit/HtmlOptimizerMvc4
Just adding another option I do not see listed here, which is the one I was recommended using:
Html minifier command line tool
Usage:
here and here
There is an issue, however, with this tool: it leaves single line (//) comments, and it causes problems for Razor parsing, since a single line comment placed within a C# block like the following:
#{
...
...
// anything
...
}
will cause the minification output rest of the line, from this point on, to be ignored by the Razor parser, which will thus raise an error stating there it could not find the closing "}" for the block.
My workaround for this issue was to completely removing these comments from the output.
This way it works.
To do that, simply remove the RegexOptions.SingleLine from line 145:
htmlContents = Regex.Replace(htmlContents, #"//(.*?)\r?\n", ""/*, RegexOptions.Singleline*/);

Resources